I'm trying desperately to solve a programming exercise, but I keep getting stuck every time. Here is the problem...
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 the student’s ID, followed
by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale: 90%–100%, A; 80%–89.99%, B;
70%–79.99%, C; 60%–69.99%, D; and 0%–59.99%, F.
I could have done this on my own if it was static arrays, however, I need to do this problem with DYNAMIC ARRAYS. That's what is destroying my brain. My assignment is due tomorrow and I spent 3 days trying to find an answer. Please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
char gradeLetter(double percent);
int main(){
ifstream inFile;
ofstream outFile;
inFile.open("testscore.txt");
outFile.open("testscoreoutput.txt");
if (inFile.fail()){
cout << "File not found." << endl;
}
else{
cout << "File found! Working..." << endl;
}
int maxSpace = 20;
char* testAnswers = new char[maxSpace];
char* stuAnswers = new char[maxSpace + 1];
string studentID;
int i;
double score = 0;
char grade;
for (i = 0; 1 < maxSpace; i++)
{
inFile.get(testAnswers[maxSpace]);
}
while (!inFile.eof())
{
inFile >> studentID;
inFile.get();
cout << studentID << endl;
for (i = 0; i < maxSpace + 1; i++)
{
inFile.get(stuAnswers[i]);
}
for (i = 0; i < maxSpace; i++)
{
if (testAnswers[i] == stuAnswers[i])
{
score = score + 2;
}
if (stuAnswers[i] == ' ')
{
score = score;
}
if (testAnswers[i] != stuAnswers[i])
{
if (score == 0){
//do nothing
}
else{
score = score - 1;
}
}
}
outFile << "Student: " << studentID << endl;
outFile << "Answers: ";
for (i = 0; i < maxSpace; i++){
outFile << stuAnswers[i];
}
double percent = (score / 40) * 100;
outFile << "Test Score: " << percent << "%" << endl;
outFile << "Grade :" << gradeLetter(percent) << endl;
}
delete[] stuAnswers;
delete[] testAnswers;
inFile.close();
outFile.close();
return 0;
}
char gradeLetter(double percent)
{
if (percent >= 90)
return 'A';
else if (percent >= 80)
return 'B';
else if (percent >= 70)
return 'C';
else if (percent >= 60)
return 'D';
else
return 'F';
}
|