teacher at your school needs help in grading a True/False test lay tu store the
ID: 3913768 • Letter: T
Question
teacher at your school needs help in grading a True/False test lay tu store the string.) rhe history The students' IDs and test ans file contains answers to the test in the form: TEFTFFTTTTFFTFTFTFTT ry other entry in the file is the student ID, followed by a blank, followed Eve by the student's responses. For example, the entry: ABC54301 TFTFTFTT TETFTFFTTFT indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted, and no answer gets zero points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scale: 908-1008, A; 80%- 89.99%, B; 70%-79.99%, C; 60%-69.99%, D; and 08-59.998, F. mes of five candidates inExplanation / Answer
The code will be
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int NUM_QUESTION = 20;//NUMBER of questions
const int NUM_STUDENTS = 200;//specify the number of students in class
//function prototypes
void marks(char studentAnswers[][NUM_QUESTION], string studentIDs[], char correctAnswers[]);
char grade(int score);
int main()
{
string studentIDs[NUM_STUDENTS];
char correctAnswers[NUM_QUESTION+1];
char studentAnswers[NUM_STUDENTS][NUM_QUESTION];
ifstream file;
file.open("");//specify path to the file here
if (!file){
cout << "There was an error opening the input file" << endl;
return 0;
}
// Read the correct answers first
file.getline(correctAnswers, '/n');
// Read each students ID and answers
for (int i = 0; i < NUM_STUDENTS; i++) {
file >> studentIDs[i]; // Take the student IDs
file.get(); // Remove the space between the student ID and the answers
for (int j = 0; j < NUM_QUESTION; j++)
studentAnswers[i][j] = file.get(); //Input the student answers
}
marks(studentAnswers, studentIDs, correctAnswers);//print the scores and grades for students
return 0;
}
void marks(char studentAnswers[][NUM_QUESTION], string studentIDs[], char correctAnswers[]) {
for (int i = 0; i < NUM_STUDENTS; i++) {
cout << "Student ID: " << studentIDs[i] << endl;
int score = 0;
cout << "Answers: ";
for (int j = 0; j < NUM_QUESTION; j++) {
cout << studentAnswers[i][j];
if (studentAnswers[i][j] == correctAnswers[j])
score += 2; // Correct answer
else if (studentAnswers[i][j] != correctAnswers[j] && studentAnswers[i][j] != ' ')
score -= 1; // Incorrect answer but not a blank
}
if (score < 0)
score = 0; // Don't allow for minus scores
cout << endl;
char g = grade(score);
cout << "Grade: " << g << endl << endl;
}
}
char grade(int score) {
double percentage = static_cast<double>(score) / (NUM_QUESTION*2);
cout << "Score: " << percentage*100 << "%" << endl;
if (percentage >= 0.9)
return 'A';
else if (percentage >= 0.8)
return 'B';
else if (percentage >= 0.7)
return 'C';
else if (percentage >= 0.6)
return 'D';
else
return 'F';
}
Do give a thumbs up
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.