Question

In: Computer Science

17.1 Lab Lesson 10 (Part 1 of 2) Part of lab lesson 10 There are two...

17.1 Lab Lesson 10 (Part 1 of 2)

Part of lab lesson 10

There are two parts to lab lesson 10. The entire lab will be worth 100 points.

Bonus points for lab lesson 10

There are also 10 bonus points. To earn the bonus points you have to complete the Participation Activities and Challenge Activities for zyBooks/zyLabs unit 16 (Gaddis Chapter 7). These have to be completed by the due date for lab lesson 10. For example, if you complete 89% of the activities you will get 8 points (there is no rounding).

Lab lesson 10 part 1 is worth 50 points

For part 1 you will have 40 points if you enter the program and successfully run the program tests. An additional 10 points will be based on the style and formatting of your C++ code.

Style points

The 10 points for coding style will be based on the following guidelines:

  • Comments at the start of your programming with a brief description of the purpose of the program.
  • Comments throughout your program
  • Proper formatting of your code (follow the guidelines in the Gaddis text book, or those used by your CS 1336 professor)
  • If you have any variables they must have meaningful names.

Development in your IDE

For lab lesson 10 (both parts) you will be developing your solutions using an Integrated Development Environment (IDE) such as Visual Studio, Code::Blocks or Eclipse. You should use whatever IDE you are using for your CS 1336 class. Once you have created and tested your solutions you will be uploading the files to zyBooks/zyLabs. Your uploaded file must match the name specified in the directions for the lab lesson. You will be using an IDE and uploading the appropriate files for this and all future lab lessons.

The filename for your source file must be lesson10part1.cpp.

You will need to develop and test the program in your IDE. Once you are satisfied that it is correct you will need to upload the source file to zyBooks/zyLabs, and submit it for the Submit mode tests. If your program does not pass all of the tests you need to go back to the IDE, and update your program to fix the problems you have with the tests. You must then upload the program from the IDE to zyBooks/zylabs again. You can then run the tests again in Submit mode.

When running your program in Submit mode it is very important that you look at the output from all of the tests. You should then try and fix all of the problems in your IDE and then upload the updated code to zyBooks/zyLabs.

C++ requirements

  • The program must make use of two parallel arrays. Each array is of type char or unsigned char. The size of each array is 30.
  • Your program must properly check for end of file.
  • Your program must properly open and close all files.
  • You are required to have your main function and two additional functions. One of the two additional functions will be a read function and the other will be a display function.

Failure to follow the C++ requirements could reduce the points received from passing the tests.

General overview

In part 1 you will be creating a quiz grading program. You will compare the student's answers with the correct answers, and determine if the student passed the quiz or not.

The program will make use of two parallel arrays.

Each array must be able to support up to 30 characters (so you have two arrays of char or unsigned char values, each one with 30 elements).

Your program will need to read in the student answer input file name from cin. It will also need to read in the correct answer file name from cin. You should use the >> operator to read from cin and not get or getline. Using get or getline will be more difficult and will require that you filter out white-space characters in your program.

The contents of the student input file should be read into one char array. The file will contain up to 30 characters, each character on a separate line of the file. The first line in the input file will contain the students answer to the first question, the second line will contain the student's answer to the 2nd question, and so on for up to 30 questions. The answers are A, B, C or D (all upper case).

The answer file will be read in as well, one answer per line for up to 30 answers. The first line of text is the answer for question 1, then second line is the answer for question 2, and so on.

You need to keep track of the number of student answers and the correct answers. If the number of student answers and correct answers is not the same you need to output an error message (see below).

As always, close the input files when you are done with them.

Your program needs to determine the number of questions that the student missed and then display the following:

  • A list of questions missed by the student, showing the question number (1 through up to 30), the correct answer, and the incorrect answer.
  • The total number of questions missed by the student
  • The percentage of the questions answered correctly.
  • If the percentage of questions answered correctly is 70% or more indicate that the student passed, otherwise state that the student failed the quiz.

You need to have at least the following three functions, including main.

Read function

One function needs to be passed a char or unsigned char array of size 30, it also needs to be passed an input file name. The return from the function should be an int value.

If the number of input values is > 30 return 30. Otherwise return the number of values read in. If the file does not exist return -1.

Work/Display function

The work/display function will be passed the two arrays, the number of student answers and correct answers read in, and should calculate and display the results from the program.

The function needs to do the following:

  • A list of questions missed by the student, showing the question number (1 through up to 30), the correct answer, and the incorrect answer.
  • The total number of questions missed by the student
  • The percentage of the questions answered correctly.
  • If the percentage of questions answered correctly is 70% or more indicate that the student passed, otherwise state that the student failed the quiz.

The main function

The main will prompt for the student answers file name and call the read function.

If the read function fails the program should output an error message and quit.

If the first read works the main should prompt for an read in the correct answers file name and call the read function a second time.

If the read fails an error message should be displayed and main should quit.

If the number of student answers is not the same as the number of correct answers an error message should be displayed and main should quit.

If the number of student answers and correct answers are the same but are both 0 display an error message and quit.

If your program gets this far call the work/display function.

As always you cannot use any global variables in your program.

See the sample runs for examples of the prompts, error messages, and results output.

See the sample runs for the output requirements.

Sample run 1 (valid data)

Contents of cin:

student.txt
answers.txt

Contents of student.txt:

A
B
C
D
A
B
C
D
A
B
C
D
A
B
C
D
A
B
C
D

Contents of answers.txt:

A
A
A
A
B
B
B
B
C
C
C
C
D
D
D
D
A
B
C
D

Here is the output to cout:

Enter student answers file name
Enter correct answer file name
Question 2 has incorrect answer 'B', the correct answer is 'A'
Question 3 has incorrect answer 'C', the correct answer is 'A'
Question 4 has incorrect answer 'D', the correct answer is 'A'
Question 5 has incorrect answer 'A', the correct answer is 'B'
Question 7 has incorrect answer 'C', the correct answer is 'B'
Question 8 has incorrect answer 'D', the correct answer is 'B'
Question 9 has incorrect answer 'A', the correct answer is 'C'
Question 10 has incorrect answer 'B', the correct answer is 'C'
Question 12 has incorrect answer 'D', the correct answer is 'C'
Question 13 has incorrect answer 'A', the correct answer is 'D'
Question 14 has incorrect answer 'B', the correct answer is 'D'
Question 15 has incorrect answer 'C', the correct answer is 'D'
12 questions were missed out of 20
The student grade is 40.0%.
The student failed

Sample run 2 (invalid student file)

Contents of cin:

invalidfile.txt
answers.txt

Here is the output to cout:

Enter student answers file name
File "invalidfile.txt" could not be opened

Sample run 3 (invalid answer file)

Contents of cin:

student.txt
invalidfile.txt

Here is the output to cout:

Enter student answers file name
Enter correct answer file name
File "invalidfile.txt" could not be opened

Sample run 4 (questions and answers are not the same)

Contents of cin:

student.txt
answer2.txt

Here is the output to cout:

Enter student answers file name
Enter correct answer file name
The student answers file has 20 entries and the correct answers file has 21 entries
Grading cannot be done if they are not the same

Note that the error message is on two lines. You must output two lines for the error messages.

Sample run 5 (the number of questions and answers is 0)

Contents of cin:

student.txt
answer2.txt

Here is the output to cout:

Enter student answers file name
Enter correct answer file name
The number of student answers and correct answers are both 0
No grade can be calculated

Note that the error message is on two lines. You must output two lines for the error messages.

Sample run 6 (all answers valid)

Assume the student input file and the answers file both have 15 values and the answers file has all correct answers. The output to cout would be:

Enter student answers file name
Enter correct answer file name
0 questions were missed out of 15
The student grade is 100.0%
The student passed

Failure to follow the requirements for lab lessons can result in deductions to your points, even if you pass the validation tests. Logic errors, where you are not actually implementing the correct behavior, can result in reductions even if the test cases happen to return valid answers. This will be true for this and all future lab lessons.

Expected output

There are 14 tests. Each test will have a new set of input data. You must match, exactly, the expected output.

You will get yellow highlighted text when you run the tests if your output is not what is expected. This can be because you are not getting the correct result. It could also be because your formatting does not match what is required. The checking that zyBooks does is very exacting and you must match it exactly. More information about what the yellow highlighting means can be found in course "How to use zyBooks" - especially section "1.4 zyLab basics".

Finally, do not include a system("pause"); statement in your program. This will cause your verification steps to fail.

Note: that the system("pause"); command runs the pause command on the computer where the program is running. The pause command is a Windows command. Your program will be run on a server in the cloud. The cloud server may be running a different operating system (such as Linux).

Error message "Could not find main function"

Now that we are using functions some of the tests are unit tests. In the unit tests the zyBooks environment will call one or more of your functions directly.

To do this it has to find your main function.

Right now zyBooks has a problem with this when your int main() statement has a comment on it.

For example:

If your main looks as follows:

int main() // main function

You will get an error message:

Could not find main function

You need to change your code to:

// main function
int main()

If you do not make this change you will continue to fail the unit tests.

Solutions

Expert Solution

Note : See the below pic to know where we have to paste the input file inorder the program to locate the input file.Thank u

___________________________

_____________________________

// stdAns.txt

A
B
C
D
A
B
C
D
A
B
C
D
A
B
C
D
A
B
C
D

_________________________

ans.txt

A
A
A
A
B
B
B
B
C
C
C
C
D
D
D
D
A
B
C
D

_______________________

// student6.txt

E
A
A
B
B
B
B
A
C
C
C
D
D
D
D
C
A
B
D
E
A
B
B
C
C
D
E
D
A
D

_______________________

// student5.txt

A
A
A
A
B
B
B
B
C
C
C
C
D
D
D
D
A
B
C
D
A
B
B
A
C
D
C
D
A
D

_______________________

// answers30.txt

A
A
A
A
B
B
B
B
C
C
C
C
D
D
D
D
A
B
C
D
A
B
B
A
C
D
C
D
A
D

_________________________

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
bool readfile(ifstream& dataIn, string infile, char arr[],int &fileCnt);
bool is_empty(std::ifstream& pFile);
bool checkFileEmpty(ifstream& dataIn, string infile);
void checkCount(int file1Cnt,int file2Cnt);
int main()
{
       //setting the precision to two decimal places
   std::cout << std::setprecision(2) << std::fixed;
string infile1, infile2;
int file1Cnt=0,file2Cnt=0;
// defines an input stream for the data file
ifstream dataIn;
int correctCnt = 0;
double percent;
const int size = 30;
// Defines an output stream for the data file
ofstream dataOut;
cout << "Enter the infile#1:";
cin >> infile1;
cout << "Enter the infile#2:";
cin >> infile2;
char studAns[size];
char answers[size];
  
  
if (readfile(dataIn, infile1, studAns,file1Cnt))
{
  
dataIn.close();
if (readfile(dataIn, infile2, answers,file2Cnt))
{
   dataIn.close();
     
   checkCount(file1Cnt,file2Cnt);
  

  
           for (int i = 0; i < file1Cnt; i++)
{
if (studAns[i] != answers[i])
{
cout << "Question " << (i + 1) << " has incorrect answer '" << studAns[i]
<< "', the correct answer is '" << answers[i] << "'" << endl;
}
else
{
correctCnt++;
}
}
  
  
if (correctCnt != file1Cnt)
{
cout << file1Cnt - correctCnt << " questions were missed" << endl;
}
else
{
cout << file1Cnt - correctCnt << " questions were missed out of "<<file1Cnt<<endl;
}
percent = (((double)correctCnt) / file1Cnt) * 100;
  
cout<<"The student grade is "<<percent<<"%."<<endl;
  
if (percent < 70)
{
cout << "The Student failed" << endl;
}
else
{
cout << "The Student passed" << endl;
}
      
  
  
}

else
{
cout << "File \"" << infile2 << "\" could not be opened" << endl;
}
}
else
{
cout << "File \"" << infile1 << "\" could not be opened" << endl;
}

return 0;
}
bool readfile(ifstream& dataIn, string infile, char arr[],int &fileCnt)
{

   dataIn.open(infile.c_str());
   if(dataIn.fail())
   return false;
  
   bool b=checkFileEmpty(dataIn,infile);
if(!b)
{
   exit(0);
   }
  
char ans;
int i = 0;

while (dataIn >> ans)
{
arr[i] = ans;

i++;
  
fileCnt++;
  

}
  
  
  
  
return true;
}

bool is_empty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}
bool checkFileEmpty(ifstream& dataIn, string infile)
{
   // dataIn.open(infile.c_str());
if (is_empty(dataIn))
   {
       cout<<"The number of student answers and correct answers are both 0"<<endl;
           cout<<"No grade can be calculated"<<endl;
       dataIn.close();
       return false;
       }
       return true;
}
void checkCount(int file1Cnt,int file2Cnt)
{
if(file1Cnt!=file2Cnt)
{
   cout<<"The student answers file has "<<file1Cnt<<" entries and the correct answers file has "<<file2Cnt<<" entries"<<endl;
   cout<<"Grading cannot be done if they are not the same"<<endl;
   exit(0);   
   }

}

_________________________

Output#1:

_________________________

Output#2:

________________________

Output#3:

__________________________

Output#4:

_______________________________

Output#5:

_______________________Thank You


Related Solutions

Binary Search Tree (Part 1): Note: This is the first part of a 2-part lab. Only...
Binary Search Tree (Part 1): Note: This is the first part of a 2-part lab. Only this part is due on Oct 29. But please get it working or you will struggle with the second part. For the second part, the data type will be changing. For now, it is a string. Contents Explanation: 2 BST.cpp: 2 Code you must write: 2 bool insert(string s) (7): 2 TNode *find(string s) (4): 2 void printTreeIO(Tnode *n)(3): 2 void printTreePre(Tnode *n) (3):...
We continue from the two tables in lab 3 part 2, and practice with functions and...
We continue from the two tables in lab 3 part 2, and practice with functions and the GROUP BY statement. The data in each table should be as below Table Product: PROD_ID PROD_NAME PROD_PRICE PROD_VENDOR 1101 Table 100 2 1102 Chair 80 3 1103 Armchair 90 2 1104 Nightstand 110 1 1105 Bed 200 3 1106 Dresser 150 3 1107 Daybed 190 2 1108 Ash Table 120 2 1109 Cherry Table 130 2 1110 Table - High 100 2 1111...
We continue from the two tables in lab 3 part 2, and practice with functions and...
We continue from the two tables in lab 3 part 2, and practice with functions and the GROUP BY statement. The data in each table should be as below Table Product: PROD_ID PROD_NAME PROD_PRICE PROD_VENDOR 1101 Table 100 2 1102 Chair 80 3 1103 Armchair 90 2 1104 Nightstand 110 1 1105 Bed 200 3 1106 Dresser 150 3 1107 Daybed 190 2 1108 Ash Table 120 2 1109 Cherry Table 130 2 1110 Table - High 100 2 1111...
THIS IS A TWO PART QUESTION: PART 1: On January 1, 2017, Mania Enterprises issued 10%...
THIS IS A TWO PART QUESTION: PART 1: On January 1, 2017, Mania Enterprises issued 10% bonds dated January 1, 2017, with a face amount of $25 million. The bonds mature in 2026 (10 years). For bonds of similar risk and maturity, the market yield is 12%. Interest is paid semiannually on June 30 and December 31. You may round all amounts to the nearest $1 if necessary. Mania Enterprises - Issuer: 1. Determine the price of the bonds at...
Lesson 10 Part B KCA & KCQ Section II KCA: Experimental Exploration of Entropy Change in...
Lesson 10 Part B KCA & KCQ Section II KCA: Experimental Exploration of Entropy Change in Mixing We will first determine the entropy change in mixing heated solid with water, according to our calorimetry experiment, then the total entropy change in the phase change of ice to steam, and finally in the dissolving salt in water. Entropy Change in Mixing Heated Solid with Water We will use the data we obtained in the calorimeter experiment to determine the entropy change...
Lesson 1:  Antibiotic Resistance Question Part 1:  Please discuss and elaborate on some of the main drivers or...
Lesson 1:  Antibiotic Resistance Question Part 1:  Please discuss and elaborate on some of the main drivers or causes of antibiotic resistance among pathogenic bacteria.   Question Part II:  What measures are or are not being taken to address antibiotic resistance?
4. (The last two questions are linked as a part 1 and 2, and they are...
4. (The last two questions are linked as a part 1 and 2, and they are each worth 4 points). You work in the HR department of a large corporation. Productivity is going down and the corporation is losing money, and everyone knows that a major reason for this is: many workers are showing up late or not at all while their colleagues are swiping their employee cards for them. For part 1, use the Fraud Triangle to identify why...
It sells to two types of consumers: medical (type 1) and academic type 2). The lab...
It sells to two types of consumers: medical (type 1) and academic type 2). The lab equipment manufacturer identifies the following demands for its two differentiated consumers: P1= 500 - Q1 P2= 300-Q2 The marginal cost to produce and sell the equipment is $50 regardless of the consumer. Assume that the equipment company can identify each type of consumer before the point of scale. a. What would be the optimal two-part pricing strategy for each type of consumer? b. Which...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and a double for the price. 2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object. 3. The Soda class...
5. In Procedure II of Part 2 of the lab you will add the correct ratio...
5. In Procedure II of Part 2 of the lab you will add the correct ratio of water to ice so that the final temperature of the water in the calorimeter is 0°C. Suppose you do this and get the following data: Mass of ice 23.5 g Mass of water originally in the calorimeter 73.79 g Initial temperature of water 25°C Final temperature of water and melted ice 0°C Determine ?Hfusion of water given these data. ----->5.92 kJ/mol 6. Given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT