You are asked to grade a multiple choice test for a class Th
You are asked to grade a multiple choice test for a class. The students’ answers are stored in a file. Each student record in the file includes a student name and the test answers given by the student. A blank space indicates that the student did not answer the question. For example, the following line represents a student’s record.
George Bush cCDdaab ccaADdc
In the above sample input, George Bush did not answer question 8. The test answer key is entered in the first line of the input file (ACDBCBDCCACDACC).
Write a C++ program that grades and determines the scores and letter grade of students’ multiple-choice tests. The number of test questions must be determined by reading the answer key string (array of characters) at the top of the input file. You may assume that there are no more than 50 questions. Your program must work for any number of students. Output the following:
• A grade report for each student that is similar to the sample output below. Replace a missing answer with a \'-\'.
• The test average and letter grade.
• The names and averages of the students with the highest grades.
Required Functions(outside of the main function):
• Write a function that grades a single student score and returns the score.
• Write a function that returns a letter grade for a score.
• Write a function that outputs a single student’s report to the screen.
Sample output:
Washington, George
-----------------------------------------------------
Answers, correct answer in parenthesis
1: a(a) 2: d(c) 3: d(d) 4: b(b) 5: c(c)
6: b(b) 7: d(d) 8: c(c) 9: -(c) 10: a(a)
11: c(c) 12: c(d) 13: a(a) 14: c(c) 15: c(c)
Score: 80.0% B
-----------------------------------------------------
Adams, John
-----------------------------------------------------
Answers, correct answer in parenthesis
1: -(a) 2: c(c) 3: d(d) 4: b(b) 5: -(c)
6: b(b) 7: d(d) 8: -(c) 9: c(c) 10: a(a)
11: c(c) 12: -(d) 13: a(a) 14: c(c) 15: -(c)
Score: 66.7% D
----------------------------------------------------- ... ...
Class average: 76.4% C
Students with the highest grade (100%): Barack Obama Richard Nixon
Sample input file
ACDBCBDCCACDACC
George Washington aDdbcBdc accAcc
John Adams cdb bd cac ac
Thomas Jefferson abcddaacbbcddda
Ronald Regan bcdbcbDccACdacc
John Kennedey cbbadcbbacdbadc
Jimmy Carter abdcad abdcbacd
George Bush CCDDAABBCCAADDC
Barack Obama acdbcbdccacdacc
Bill Clinton ACDBCBDCCACAACC
Richard Nixon acdbcacccacdacc
Solution
#include<iostream>
#include<fstream>
#include<cstring>
#include <cctype>
using namespace std;
int gradeStudent(char[],char[]);
char getLetterGrade(float);
int processFile(char[][200],char[]);
void processStudent(char[], char[][100],char[]);
float getGrade(int,int);
int main()
{
char lines[100][200],sentence[200],name[3][100],answers[200],key[200],letterGrade;
int totalLines = 0, totalQuestions = 0, correctAnswers = 0;
float grade;
totalLines = processFile(lines,key);
cout<<\"The key is: \"<<key<<\"\ \ \";
totalQuestions = strlen(key);
for(int i=1;i<totalLines;i++){
processStudent(lines[i],name,answers);
cout<<name[1]<<\",\"<<name[0]<<\"\ \ \";
cout<<\"--------------------------------------------------------------\ \ \";
cout<<\"Answers, correct answer in parenthesis\ \ \";
correctAnswers = gradeStudent(answers,key);
grade = getGrade(totalQuestions,correctAnswers);
letterGrade = getLetterGrade(grade);
cout<<\"\ \ Score : \"<<grade<<\"% \"<<letterGrade<<\"\ \ \";
cout<<\"--------------------------------------------------------------\ \ \";
}
return 0;
}
int processFile(char lines[100][200],char key[200]){
int i = 0;
ifstream myfile(\"test.txt\");
if(!myfile)
{
cout<<\"Error opening output file\ \ \";
return -1;
}
while(!myfile.eof())
{
myfile.getline(lines[i],180);
if(i==0){
strcpy(key,lines[i]);
}
i++;
}
return i-1;
}
int gradeStudent(char answerString[200],char key[200]){
int correctAnswers = 0;
for(int i=0;i<strlen(key);i++){
if(tolower(answerString[i])==tolower(key[i])){
correctAnswers++;
}
if(answerString[i]==\'\')answerString[i] = \'-\';
cout<<i<<\" : \"<<(char)tolower(answerString[i])<<\" (\"<<(char)tolower(key[i])<<\")\\t\";
}
return correctAnswers;
}
float getGrade(int totalQuestions,int correctAnswers){
float percentage = 0.00;
percentage = ((float)correctAnswers/totalQuestions)*100.00;
return percentage;
}
char getLetterGrade(float percentage){
if(percentage>=0 && percentage<40)return \'E\';
if(percentage>=40 && percentage<60)return \'D\';
if(percentage>=60 && percentage<75)return \'C\';
if(percentage>=75 && percentage<85)return \'B\';
if(percentage>=85 && percentage<100)return \'A\';
}
void processStudent(char sentence[200], char name[3][100],char answers[200]){
int length,spaceCounter = 0,j = 0,k = 0;
strcpy(name[0],\"\");
strcpy(name[1],\"\");
for(int i=0;i<strlen(sentence);i++){
if(spaceCounter==0){
name[0][i] = sentence[i];
}else if(spaceCounter==1){
if(j==0)strcat(name[0],\"\");
name[1][j++] = sentence[i];
} else {
if(k==0)strcat(name[1],\"\");
answers[k++] = sentence[i];
}
if(sentence[i]==\' \')spaceCounter++;
}
return;
}


