C++ The history teacher at your school needs help in grading a True/False test.
ID: 3715783 • Letter: C
Question
C++
The history teacher at your school needs help in grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:
TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student ID, followed by a blank, followed by the student’s responses. For example, the entry:
ABC54301 TFTFTFTT TFTFTFFTTFT
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 in the form of a table that shows the student’s ID, followed by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale:
A: 90%–100%
B: 80%–89.99%
C: 70%–79.99%
D: 60%–69.99%
F: 0%–59.99%
Using the data file provided, here is what your output should look like:
Key: TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF 27 D
DEF56278 TTFTFTTTFTFTFFTTFTTF 40 A
ABC42366 TTFTFTTTFTFTFFTTF 34 B
ABC42586 TTTTFTTT TFTFFFTF 26 D
This is the input file
Explanation / Answer
Solution:
code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <math.h>
#include <vector>
using namespace std;
const int MAXQUESTIONS = 20;
const int TOTAL_STUDENTS = 151;
char letterGrade(int totalScore)
{
double scorePercent = static_cast<double>(totalScore) / (MAXQUESTIONS*2);
cout << "Score: " << scorePercent*100 << "%" << endl;
if (scorePercent >= 0.9)
return 'A';
else if (scorePercent >= 0.8)
return 'B';
else if (scorePercent >= 0.7)
return 'C';
else if (scorePercent >= 0.6)
return 'D';
else
return 'F';
}
int main()
{
char grade;
string ID[TOTAL_STUDENTS];
char answerCorrect[MAXQUESTIONS+1];
char answersGiven[TOTAL_STUDENTS][MAXQUESTIONS];
ifstream inputFile;
inputFile.open("Ch8_Ex6Data.txt");
if (!inputFile)
{
cout << "Error opening the input file" << endl;
exit(1);
}
// get the correct answers
inputFile.getline(answerCorrect, '/n');
int i = 0;
while(true)
{
inputFile >> ID[i];
inputFile.get();
for (int j = 0; j < MAXQUESTIONS; j++)
answersGiven[i][j] = inputFile.get();
if(inputFile.eof())
break;
i++;
}
int totalStudents = i;
for (int i = 0; i < totalStudents; i++)
{
cout << " Student ID: " << ID[i] << endl;
int totalScore = 0;
cout << "Answers: ";
for (int j = 0; j < MAXQUESTIONS; j++)
{
cout << answersGiven[i][j];
if (answersGiven[i][j] == answerCorrect[j])
totalScore = totalScore + 2;
else if (answersGiven[i][j] != answerCorrect[j] && answersGiven[i][j] != ' ')
totalScore = totalScore - 1;
}
if (totalScore < 0)
totalScore = 0;
cout << endl;
grade = letterGrade(totalScore);
cout << "Grade: " << grade << endl << endl;
}
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.