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
|
#include<iostream>
#include<fstream>
using namespace std;
int compareAns(char [], int, char);
int calcAverage(int);
int main()
{
const int SIZE_ONE = 20;
const int SIZE_TWO = 220;
char correct[SIZE_ONE];
char student[SIZE_TWO];
int questCount = 1, stuCount = 1, // questCount is counter for times for loop has ran, stuCount increases every 20 questCounts
ansCount = 0, i = 0, // ansCount is counter for wrong answers
answers, average; // answers is variable for compareAns function, average is variable for calcAverage function
ifstream inputFile1, inputFile2;
inputFile1.open("CorrectAnswers.txt");
while (i < SIZE_ONE && inputFile1 >> correct[i])
i++;
inputFile1.close();
i = 0;
inputFile2.open("StudentAnswers.txt");
while (i < SIZE_TWO && inputFile2 >> student[i])
i++;
inputFile2.close();
for (i = 0; i < SIZE_TWO; i++)
{
answers = compareAns(correct, SIZE_ONE, student[i]);
if (answers >= 0)
ansCount++;
if (questCount % 20 == 0)
{
average = calcAverage(ansCount);
cout << "Report for Student " << stuCount << endl;
cout << "---------------------" << endl;
cout << "Missed " << ansCount << " out of 20 questions "
<< "for " << average << "% correct." << endl;
cout << "Questions missed:" << endl;
cout << " " << endl;
if (average >= 70)
cout << "This student passed the exam!" << endl << endl;
else
cout << "This student failed the exam." << endl << endl;
stuCount++;
ansCount = 0;
}
questCount++;
}
return 0;
}
// Description of compareAns:
// Acts as a linear search algorithm to compare the two arrays
// for correct answers and student answers. Return value based off if
// the two answers are equal or not.
int compareAns(char correctAns[], int size, char stdAns)
{
for (int j = 0; j < size; j++)
{
if (stdAns == correctAns[j])
return j;
}
return -1;
}
int calcAverage(int wrongAns)
{
int correctAns, avg;
correctAns = 20 - wrongAns;
avg = correctAns / 20 * 100;
return avg;
}
|