teacher at your school needs help in grading a True/False test lay tu store the
ID: 3915413 • 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
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
// Function to read two numbers from StudentAnswers.txt and CorrectAnswers.txt file
int readTextFile(string name[], string &cAns, string stAns[])
{
// To store number of records
int counter = 0;
//ifstream object created
ifstream inFileS, inFileA;
// Opens file for reading data from CorrectAnswer.txt file
inFileA.open ("CorrectAnswer.txt");
// Opens file for reading data from StudentAnswer.txt file
inFileS.open ("StudentAnswers.txt");
// Reads answers from the file and stores it in cAns
std::getline(inFileA, cAns);
// Close the file
inFileA.close();
// Loops till end of the file
while(!inFileS.eof())
{
// Reads data up to single space for student id
getline(inFileS, name[counter], ' ');
// Reads student answer
getline(inFileS, stAns[counter]);
// Increase the record counter by one
counter++;
}// End of while
// Close the file
inFileS.close();
// Returns number of records
return counter;
}//End of function
// Function to calculate wrong answers and stores the index position of the wrong answers
void calculateWrong(string cAns, string stAns[], int missed[][20], int no)
{
// Loops till end of number of students
for(int c = 0; c < no; c++)
{
// Initializes wrong answer counter to zero for each student
int missedC = 0;
// Loops through 20 answers
for(int d = 0; d < 20; d++)
{
// Checks character at d index position of the answer is not equals to student at c index position and answer at d index position
if(cAns.at(d) != stAns[c].at(d))
{
// Stores the answer index d in missed matrix c index position for student and missedC index position for wrong answer position
missed[c][missedC++] = d;
}// End of else if
}// End of inner for loop
}// End of outer for loop
}// End 0f function
// Function to display student result
void displayGrade(string name[], string cAns, string stAns[], int missed[][20], int no)
{
// Counter for number of wrong answers
int wrong;
// To store the percentage
float percent;
// To store maximum and minimum percentage
// Assumes 0 as the initial maximum percentage and 100 as the minimum percentage
int maxPer = 0, minPer = 100;
// To store the index position of the student for maximum and minimum percentage secured
// Assumes -1 initially
int maxP = -1, minP = -1;
// Loops till end of number of students
for(int c = 0; c < no; c++)
{
// Initializes wrong answer counter to zero for each student
wrong = 0;
// Initializes percentage to zero for each student
percent = 0;
// Displays student number and name
cout<<" Report for Student "<<(c + 1)<<" "<<name[c]<<": ";
cout<<" ---------------------------------------";
// Loops till number of answers
for(int d = 0; d < 20; d++)
// Checks if the matrix index position c for student and d for answer is not -1
if(missed[c][d]!= -1)
// Increase the wrong counter by one
wrong++;
// Calculates percentage
percent = ((20 - wrong) / 20.0) * 100;
// Displays number of wrong answers and percentage
cout<<" Missed "<<wrong<<" out of 20 questions for "<<percent<<"% correct.";
// Checks if number of wrong answer is not zero
if(wrong != 0)
// Then display the message
cout<<" Question Missed: "<<endl;
// Loops till number of answers
for(int d = 0; d < 20; d++)
{
// Checks if the matrix index position c for student and d for answer is not -1
if(missed[c][d]!= -1)
// Display the wrong answer given by the student and the correct answer
cout<<missed[c][d]+1<<"("<<cAns.at(d)<<"/"<<stAns[c].at(d)<<"), ";
}// End of for loop
// Checks if the percentage is greater than or equals to 70
if(percent >= 70)
// Display pass message
cout<<" The student "<<name[c]<<" passed the exam!";
// Otherwise display fail message
else
cout<<" The student "<<name[c]<<" failed the exam!";
// Checks if the current student percentage is greater than the earlier maximum percentage
if(percent > maxPer)
{
// Update the maximum percentage with current student percentage
maxPer = percent;
// Update the index position of maximum to c value
maxP = c;
}// End of if condition
// Checks if the current student percentage is less than the earlier minimum percentage
if(percent < minPer)
{
// Update the minimum percentage with current student percentage
minPer = percent;
// Update the index position of minimum to c value
minP = c;
}// End of if condition
}// End of outer for loop
// Displays the student number, name and percentage for best and worst student
cout<<" Student "<<maxP<<" - "<<name[maxP]<<" had the best grade with "<<maxPer<<"%";
cout<<" Student "<<minP<<" - "<<name[minP]<<" had the wost grade with "<<minPer<<"%";
}// End of function
// Main function definition
int main()
{
// To store correct answers from file
string correctAnswers;
// To store student ID from file
string studentName[20];
// To store student answers from file
string studentAnswer[20];
// To store each student wrong answer
int missed[20][20];
// Loops 20 times for students
for(int x = 0; x < 20; x++)
// Loops 20 times for answers
for(int y = 0; y < 20; y++)
// Initializes each position to -1
missed[x][y] = -1;
// Calls the function to read data from file
int numberOfStudents = readTextFile(studentName, correctAnswers, studentAnswer);
// Calls the function to calculate number of wrong answers
calculateWrong(correctAnswers, studentAnswer, missed, numberOfStudents);
// Calls the function to display each students result
displayGrade(studentName, correctAnswers, studentAnswer, missed, numberOfStudents);
}//En of main
Sample Output:
Report for Student 1 Ram:
---------------------------------------
Missed 10 out of 20 questions for 50% correct.
Question Missed:
6(T/T), 7(F/F), 8(F/F), 9(T/T), 10(T/T), 11(T/F), 14(F/T), 15(T/F), 16(F/T), 18(T/F),
The student Ram failed the exam!
Report for Student 2 Syam:
---------------------------------------
Missed 2 out of 20 questions for 90% correct.
Question Missed:
16(T/T), 17(F/F),
The student Syam passed the exam!
Report for Student 3 Suresh:
---------------------------------------
Missed 1 out of 20 questions for 95% correct.
Question Missed:
10(T/T),
The student Suresh passed the exam!
Report for Student 4 Sita:
---------------------------------------
Missed 4 out of 20 questions for 80% correct.
Question Missed:
12(T/T), 15(F/F), 19(F/F), 20(T/T),
The student Sita passed the exam!
Report for Student 5 Gita:
---------------------------------------
Missed 11 out of 20 questions for 45% correct.
Question Missed:
1(T/F), 4(F/F), 5(F/F), 6(T/F), 8(T/F), 10(T/F), 13(F/F), 14(T/F), 16(F/F), 17(T/F), 18(F/F),
The student Gita failed the exam!
Report for Student 6 Rohan:
---------------------------------------
Missed 7 out of 20 questions for 65% correct.
Question Missed:
2(T/T), 4(F/T), 9(F/F), 12(T/F), 14(T/T), 16(T/T), 18(F/F),
The student Rohan failed the exam!
Report for Student 7 Rahim:
---------------------------------------
Missed 13 out of 20 questions for 35% correct.
Question Missed:
1(T/F), 2(F/T), 3(F/T), 4(T/F), 5(T/F), 6(T/F), 8(F/F), 11(T/F), 13(F/F), 16(T/T), 17(F/T), 19(F/F), 20(T/F),
The student Rahim failed the exam!
Report for Student 8 Rakesh:
---------------------------------------
Missed 9 out of 20 questions for 55% correct.
Question Missed:
1(T/F), 2(F/T), 5(F/F), 7(T/T), 12(T/F), 13(T/T), 14(F/T), 17(T/T), 20(F/F),
The student Rakesh failed the exam!
Report for Student 9 PYARI:
---------------------------------------
Missed 0 out of 20 questions for 100% correct.
The student PYARI passed the exam!
Student 8 - PYARI had the best grade with 100%
Student 6 - Rahim had the wost grade with 35%
CorrectAnswer.txt file contents
TFFTTTFTFTFFTTFTTTFF
StudentAnswers.txt
Ram TFFTTFTFTFTFTFTFTFFF
Syam TFFTTTFTFTFFTTFFFTFF
Suresh TFFTTTFTFFFFTTFTTTFF
Sita TFFTTTFTFTFTTTTTTTTT
Gita FFFFFFFFFFFFFFFFFFFF
Rohan TTFFTTFTTTFTTFFFTFFF
Rahim FTTFFFFFFTTFFTFFFTTT
Rakesh FTFTFTTTFTFTFFFTFTFT
PYARI TFFTTTFTFTFFTTFTTTFF
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.