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 144 145 146 147 148 149 150
|
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double mean(int v[], int size);
// PRE: size of array > 0
// POST: return average of values in array
double stddev(int v[], int size, double m);
// PRE: size of array > 0, 0 <= m <= 100
// POST: return standard deviation of values in array with mean m
char letter(int score, double m, double s);
// PRE: 0 <= score, m, s <= 100
// POST: return letter grade for score with mean m and standard deviation s
string code(string id, string lastname, string firstname);
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id
int main()
{
ifstream fin;
ofstream fout;
fin.open("grades.txt");
fout.open("classgrades.txt");
const int CAPACITY = 24; // capacity of the array
string codes[CAPACITY]; // array of id codes
int midterms[CAPACITY]; // array of midterm grades
int finals[CAPACITY]; // array of final grades
string id; // 7-digit id code
string lastname;
string firstname;
string major;
string year; // Student's year of school
int midterm;
int final;
string idcode;
double finalmean; // Average grade on the final
double finalstddev; // Standard deviation among final grades
double midtermmean; // Average grade on the midterm
double midtermstddev; // Standarn deviation among midterm grades
char finalgrade[CAPACITY]; // An array of the letter grades for the final
char midtermgrade[CAPACITY]; // An array of the letter grades for the midterm
int ct = 0;
while (!fin.fail(),ct<=24)
{
ct++;
fin >> id;
fin >> lastname;
fin >> firstname;
fin >> major;
fin >> year;
fin >> midterm;
fin >> final;
midterms[ct - 1] = midterm;
finals[ct - 1] = final;
idcode = code(id, lastname, firstname);
codes[ct - 1] = idcode;
}
finalmean = mean(finals, CAPACITY);
midtermmean = mean(midterms, CAPACITY);
finalstddev = stddev(finals, CAPACITY, finalmean);
midtermstddev = stddev(midterms, CAPACITY, midtermmean);
for (int i = 1; i <= CAPACITY; i++)
{
finalgrade[i - 1] = letter(finals[i - 1], finalmean, finalstddev);
midtermgrade[i - 1] = letter(midterms[i - 1], midtermmean, midtermstddev);
}
fout << "Grade Calculations" << endl << "By Shane Blodgett" << endl;
for (int i = 1; i <= CAPACITY; i++)
{
fout << setprecision(1);
fout << setw(15) << codes[i - 1] << setw(15) << midterms[i - 1]
<< midtermgrade[i - 1] << setw(15) << finals[i - 1]
<< finalgrade[i - 1] << endl;
}
fout << endl << endl;
fout << "The class mean for the midterm exam was a " << midtermmean << endl;
fout << "The standard deviation amongst the midterm exams was +/- " << midtermstddev << endl;
fout << endl << endl;
fout << "The class mean for the final exam was a " << finalmean << endl;
fout << "The standard deviation amongst the final exams was +/- " << finalstddev << endl;
fin.close();
fout.close();
return 0;
}
string code(string id, string lastname, string firstname)
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id
{
char firstletterfirst = firstname[0];
char firstletterlast = lastname[0];
char sixthdigitid = id[5];
char seventhdigitid = id[6];
string idcode = { firstletterfirst, firstletterlast, sixthdigitid, seventhdigitid };
return idcode;
}
double mean(int v[], int size)
// PRE: size of array > 0
// POST: return average of values in array
{
int sum = 0;
for (int i = 1; i <= size; i++)
{
sum = sum + v[i - 1];
}
double mean = sum / size;
return mean;
}
double stddev(int v[], int size, double m)
{
double sumofsquares = 0;
double spread[24];
double var;
double stddev;
for (int i = 1; i < size; i++)
{
spread[i - 1] = (v[i - 1] - m)*(v[i - 1] - m);
sumofsquares = sumofsquares + spread[i - 1];
}
var = sumofsquares / 23;
stddev = sqrt(var);
return stddev;
}
char letter(int score, double m, double s)
// PRE: 0 <= score, m, s <= 100
// POST: return letter grade for score with mean m and standard deviation s
{
char grade;
if (score < (m - (2 * s)))
grade = 'F';
else if (score >= (m - (2 * s)) && score < (m - s))
grade = 'D';
else if (score >= (m - s) && score < m)
grade = 'C';
else if (score >= m && score < (m + 0.67*s))
grade = 'B';
else
grade = 'A';
return grade;
}
|