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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes
void Initialize(int&, double&);
void GetData(ifstream&, ofstream&, string&, int&, int&, int&, int&, int&);
void FindLowest(int&, int&, int&, int&, int&, int&);
void CalcAverage(int&, int&, int&, int&, int&, int&, double&);
void PrintResults(ofstream&, string&, int&, int&, int&, int&, int&, double&);
void AccumulateClassInfo(ifstream&, int&, double&, double&);
void CalcClassAverage(int&, double&, double&);
void PrintClassInfo(ofstream&, int&, double&);
int main()
{
ifstream inData; // input file
ofstream outData; // output file
string studentID; // student ID number
int numStudents; // number of students in file
int t1; // test 1 score
int t2; // test 2 score
int t3; // test 3 score
int t4; // test 4 score
int t5; // test 5 score
int lowestTest; // lowest test score
double average; // average of 4 highest tests
double sumOfAverages; // sum of the averages of all students
double classAverage; // average for class
inData.open("a:\\studentinfo.txt");
outData.open("a:\\studentoutput.txt");
outData << fixed << showpoint;
outData << "Student ID " << "Test 1 " << "Test 2 "
<< "Test 3 " << "Test 4 " << "Test 5 " <<
"Average" << endl << endl;
//Function Calls
Initialize(numStudents, sumOfAverages);
GetData(inData, outData, studentID, t1, t2, t3, t4, t5);
while (inData)
{
if (!inData)
{
cout << "Data error: File not found." << endl;
cout << "Program terminates" << endl;
return 1;
}
else
{
FindLowest(t1, t2, t3, t4, t5, lowestTest);
CalcAverage(t1, t2, t3, t4, t5, lowestTest, average);
PrintResults(outData, studentID, t1, t2, t3, t4, t5, average);
AccumulateClassInfo(inData, numStudents, average,
sumOfAverages);
}
GetData(inData, outData, studentID, t1, t2, t3, t4, t5);
outData << endl;
} // End while loop
CalcClassAverage(numStudents, sumOfAverages, classAverage);
PrintClassInfo(outData, numStudents, classAverage);
inData.close();
outData.close();
getch();
return 0;
}
//Function Definitions
void Initialize(int& sNum, double& sumAvg)
{
sNum = 0;
sumAvg = 0.00;
}
void GetData(ifstream& inPut, ofstream& outPut, string& sID, int& test1,
int& test2, int& test3, int& test4, int& test5)
{
inPut >> sID >> test1 >> test2 >> test3 >> test4 >> test5;
}
void FindLowest(int& test1, int& test2, int& test3, int& test4,
int& test5, int& lowScore)
{
if ((test1 < test2) && (test1 < test3) && (test1 < test4)
&& (test1 < test5))
lowScore = test1;
else if ((test2 < test1) && (test2 < test3) && (test2 < test4)
&& (test2 < test5))
lowScore = test2;
else if ((test3 < test1) && (test3 < test2) && (test3 < test4)
&& (test3 < test5))
lowScore = test3;
else if ((test4 < test1) && (test4 < test2) && (test4 < test3)
&& (test4 < test5))
lowScore = test4;
else
lowScore = test5;
}
void CalcAverage(int& test1, int& test2, int& test3, int& test4,
int& test5, int& lowScore, double& avg)
{
if (test1 = lowScore)
avg = (test2 + test3 + test4 + test5) / 4;
else if (test2 = lowScore)
avg = (test1 + test3 + test4 + test5) / 4;
else if (test3 = lowScore)
avg = (test1 + test2 + test4 + test5) / 4;
else if (test4 = lowScore)
avg = (test1 + test2 + test3 + test5) / 4;
else
avg = (test1 + test2 + test3 + test4) /4;
}
void PrintResults(ofstream& outPut, string& sID, int& test1, int& test2,
int& test3, int& test4, int& test5, double& avg)
{
outPut << sID << " " << test1 <<
" " << test2 << " " << test3 << " "
<< test4 << " " << test5 << " " <<
setprecision(2) << avg << endl;
}
void AccumulateClassInfo(ifstream& inPut, int& sNum,
double& avg, double& sumAvg)
{
sNum++;
avg++;
sumAvg = (sumAvg + avg) / sNum;
}
void CalcClassAverage(int& sNum, double& sumAvg, double& classAvg)
{
classAvg = sumAvg / sNum;
}
void PrintClassInfo(ofstream& outPut, int& sNum, double& classAvg)
{
outPut << setw(15) << "Number of Students" << setw(30) <<
"Class Average" << endl << endl;
outPut << setw(15) << sNum << setw(30) << setprecision(2)
<< classAvg << endl;
}
|