This is the problem: CorrectAnswers.txt (the correct answers to the exam) is thi
ID: 3820253 • Letter: T
Question
This is the problem:
CorrectAnswers.txt (the correct answers to the exam) is this picture
The student's answers are listed in this picture
My professor would like it laid out in the following fashion:
If anything in any of the pictures are unclear, comment and I'll try to clarify. Thank you!
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>
using namespace std;
const int MAX_SIZE = 20;
class ExamData
{
public:
char correctAnswers[MAX_SIZE];
char studentAnswers[MAX_SIZE];
int numOfQuestions;
int studentNumCorrect;
};
void printHeader(string, string, string, string);
int readData(string, char[]);
void scoreExams(ExamData);
void printResults(ExamData);
int main()
{
// Call the splash screen function
printHeader("Your Name", "CMPSC 121", "Date", "Program Description");
ExamData midterm;
scoreExams(midterm);
printResults(midterm);
return 0;
}
void scoreExams(ExamData exam)
{
exam.numOfQuestions = readData("CorrectAnswers.txt", exam.correctAnswers);
int numAnswers = readData("StudentAnswers.txt", exam.studentAnswers);
exam.studentNumCorrect = 0;
for(int i = 0; i < exam.numOfQuestions; i++)
if(exam.correctAnswers[i] == exam.studentAnswers[i])
exam.studentNumCorrect++;
}
int readData(string fileName, char inArray[])
{
ifstream inStream;
int size = -1;
char tempChar;
inStream.open(fileName);
if(!inStream.fail())
{
while(inStream >> tempChar)
{
size++;
inArray[size] = tempChar;
}
}
else
{
cout << "File: " << fileName << " failed to open ";
exit(1);
}
return size+1;
}
void printHeader(string name, string course, string dueDate, string description) {
// PRINTHEADER printHeader(string, string, string, string) is the
// function used to hide away the splash screen
//
// Name: Prof. Adams
// Course: CMPSC 121
// Date: 12 March 2017
//
// Print the splash screen
cout << endl;
cout << name << endl;
cout << course << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
void printResults(ExamData exam)
{
cout << "Correct Answers: ";
for(int i = 0; i < exam.numOfQuestions; i++)
cout << exam.correctAnswers[i] << " ";
cout << endl;
cout << "Student Answers: ";
for(int i = 0; i < exam.numOfQuestions; i++)
cout << exam.studentAnswers[i] << " ";
cout << endl;
cout << "Percentage of score: " << exam.studentNumCorrect / (double)exam.numOfQuestions * 100 << "%" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.