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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// My Name
//
// Program Average calculates the average of five test scores where the
// lowest score is dropped for each student. The program also calculates
// a class average.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void Initialize(int, double);
void GetData(ifstream, 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(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("C:\\...\\data1.txt");
outData.open("C:\\...\\results.txt");
outData << fixed << showpoint;
Initialize(numStudents, sumOfAverages);
GetData(inData, studentID, t1, t2, t3, t4, t5);
if (!inData)
{
cout << "File not found" << endl;
getch();
}
else
{
while (inData)
{
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(numStudents, average, sumOfAverages);
GetData(inData, studentID, t1, t2, t3, t4, t5);
}
}
CalcClassAverage(numStudents, sumOfAverages, classAverage);
PrintClassInfo(outData, numStudents, classAverage);
inData.close();
outData.close();
return 0;
}
//**********************************************************************
void Initialize(/*out*/ int numStudents, /*out*/ double sumOfAverages)
//Preconditions: none
//Postconditions: The counter and accumulator are set to zero
{
numStudents = 0;
sumOfAverages = 0;
}
//**********************************************************************
void GetData(/*in*/ ifstream inData,
/*out*/ string& studentID,
/*out*/ int& t1,
/*out*/ int& t2,
/*out*/ int& t3,
/*out*/ int& t4,
/*out*/ int& t5)
//Preconditions: The file is open and ready to be read from
//Postconditions: The studentID and the five test grades have been read
{
while(inData)
{
inData >> studentID;
inData >> t1;
inData >> t2;
inData >> t3;
inData >> t4;
inData >> t5;
}
}
//**********************************************************************
void FindLowest(/*in*/ int t1,
/*in*/ int t2,
/*in*/ int t3,
/*in*/ int t4,
/*in*/ int t5,
/*out*/ int& lowestTest)
//Preconditions: The five test grades have been read
//Postconditions: The lowest grade is found
{
if(t1 < t2 && t1 < t3 && t1 < t4 && t1 < t5)
lowestTest = t1;
else if(t2 < t1 && t2 < t3 && t2 < t4 && t2 < t5)
lowestTest = t2;
else if(t3 < t1 && t3 < t2 && t3 < t4 && t3 < t5)
lowestTest = t3;
else if(t4 < t1 && t4 < t2 && t4 < t3 && t4 < t5)
lowestTest = t4;
else if(t5 < t1 && t5 < t2 && t5 < t3 && t5 < t4)
lowestTest = t5;
}
//**********************************************************************
void CalcAverage(/*in*/ int t1,
/*in*/ int t2,
/*in*/ int t3,
/*in*/ int t4,
/*in*/ int t5,
/*in*/ int lowestTest,
/*out*/ double& average)
//Preconditions: The five test grades have been read
// The lowest test grade is known
//Postconditions: The average of the four best grades is calculated
{
average = (t1 + t2 + t3 + t4 + t5 - lowestTest)/4;
}
//**********************************************************************
void PrintResults(/*out*/ ofstream outData,
/*in*/ string studentID,
/*in*/ int t1,
/*in*/ int t2,
/*in*/ int t3,
/*in*/ int t4,
/*in*/ int t5,
/*in*/ double average)
//Preconditions: The output file is open and ready to be written to
// The student ID and the five test grades have been read
// The average of the four best test grades has been calculated
//Postconditions: The student ID, the five test grades, and the calculated
// average (with 2 decimal points) have been written to a file
{
outData << studentID << " "
<< t1 << " " << t2 << " " << t3 << " " << t4 << " " << t5
<< " " << setprecision(2) << average << endl;
}
//**********************************************************************
void AccumulateClassInfo(/*in or out*/ int numStudents,
/*in*/ double average,
/*in or out*/ double& sumOfAverages)
//Preconditions: The current number of students is known
// The current accumulated average is known
//Postconditions: The number of students is incremented
// The new average is added to the total
{
numStudents++;
sumOfAverages = sumOfAverages + average;
}
//**********************************************************************
void CalcClassAverage(/*in*/ int numStudents,
/*in*/ double sumOfAverages,
/*out*/ double& classAverage)
//Preconditions: All data has been process and the number of students
// and the sum of class averages is known
//Postconditions: The class average is calculated
{
classAverage = sumOfAverages/numStudents;
}
//**********************************************************************
void PrintClassInfo(/*out*/ ofstream outData,
/*in*/ int numStudents,
/*in*/ double classAverage)
//Preconditions: The output file is open and all data has been processed
// The number of students and the class average is known
//Postconditions: The number of students and class average (with 2 decimal
// places) has been written to the file
{
outData << numStudents << " " << setprecision(2) << classAverage << endl;
}
|