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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include<fstream>
#include<string>
#include<iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::setw;
using std::setprecision;
using std::fixed;
int main()
{
ifstream sampleGrades( "sampleGrades.txt" );
ofstream outputGrades( "outputGrades.txt" );
int tab = 15;
string firstLine;
int totalNoStudents;
int ID = 0;
const int LIMIT = 11;
double item[LIMIT] = { 0 };
string grade[] = { "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F", "Error: If this appears you're #$%@*^~ wrong!!" };
int gradeLimit[] = { 100, 97, 93, 90, 87, 83, 80, 73, 77, 70, 60, 0 };
int gradeGroup[] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 4 };
char gradeGroupName[] = { 'A', 'B', 'C', 'D', 'F' };
int noOfGrades = sizeof( gradeLimit ) / sizeof(int) - 1;
int noOfGroups = sizeof( gradeGroupName ) / sizeof(char);
// Stats per individual grade
double *attendance = new double[noOfGrades];
int *count = new int[noOfGrades]; // Number of students attaining particular grade
// Stats per grade group
double *groupAttendance = new double[noOfGroups];
int *groupCount = new int[noOfGroups];
//Initialise the individual arrays
for (int i = 0; i < noOfGrades; i++)
{
attendance[i] = 0;
count[i] = 0;
}
//Initialise the group arrays
for (int i = 0; i < noOfGroups; i++)
{
groupAttendance[i] = 0;
groupCount[i] = 0;
}
double numberGrade = 0;
if (sampleGrades.is_open())
{
// Read header material
getline( sampleGrades, firstLine );
sampleGrades >> totalNoStudents;
outputGrades << setw(tab) << "ID" << setw(tab) << "Grade" << setw(tab) << "Score" << endl;
while ( !sampleGrades.eof() )
{
// Read student
sampleGrades >> ID;
// Read student's results
for ( int i = 0; i < LIMIT; i++ )
sampleGrades >> item[i]; // note item i is the raw score for an assessment item
// Grade results numerically
numberGrade = item[0] * 0.4 + (item[1] + item[2] + item[3] + item[4] + item[5] + item[6] + item[7]) * 0.05 + item[8] * 0.6 + item[9] * 0.6 + item[10] * 0.625;
// Determine alphabetic grading and accumulate statistics
int gradeIndex = 0;
for ( gradeIndex = 0; gradeIndex < noOfGrades; gradeIndex++ )
{
if ( numberGrade > gradeLimit[ gradeIndex + 1 ] && numberGrade <= gradeLimit[gradeIndex] )
{
attendance[gradeIndex] += item[1];
count[gradeIndex]++;
groupAttendance[ gradeGroup[ gradeIndex ] ] += item[1];
groupCount[ gradeGroup[ gradeIndex ] ]++;
break;
}
}
outputGrades << setw(tab) << ID << setw(tab) << grade[gradeIndex] << setw(tab) << fixed << setprecision(1) << numberGrade << endl;
}
outputGrades << endl;
// Detailed statistics
outputGrades << setw(tab) << "Grade" << setw(tab) << "Count" << setw(tab) << "Total Att" << setw(tab) << "Average" << endl;
double average = 0;
for ( int i = 0; i < noOfGrades; i++ )
{
if (count[i] == 0)
average = 0;
else
average = attendance[i] / count[i];
outputGrades << setw(tab) << grade[i] << setw(tab) << count[i] << setw(tab) << attendance[i] << setw(tab) << average << endl;
}
outputGrades << endl;
// Group statistics
average = 0;
for ( int i = 0; i < noOfGroups; i++ )
{
if ( groupCount[i] == 0 )
average = 0;
else
average = groupAttendance[i] / groupCount[i];
outputGrades << setw(tab) << gradeGroupName[i] << setw(tab) << groupCount[i] << setw(tab) << groupAttendance[i] << setw(tab) << average << endl;
}
// Cleanup
sampleGrades.close();
outputGrades.close();
delete[] attendance;
delete[] count;
delete[] groupAttendance;
delete[] groupCount;
}
else
cout << "Unable to open file";
return 0;
}
|