In: Computer Science
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:
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
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:
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:
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.
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