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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
const int ID_LOW = 200;
const int ID_HIGH = 299;
//Prototypes
void printHeading(ofstream& outFile);
void Rd(ifstream& inFile, string& studName, int& studId, float& totalGPA);
void printData(ofstream& outFile, string studName, int studId, float GPA);
void calcMax(float& maxOfGPA, float totalGPA);
float avg(float sum, int numOfRecords);
void printStatistic(ofstream& outFile, float maxOfGPA, float avgOfGPA);
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("in.data");
outFile.open("out.data");
outFile.setf(ios::fixed);
outFile.precision(2);
string studName;
int studId;
int numOfRecords = 0;
float totalGPA = 0.0;
float maxOfGPA = 0.0;
float avgOfGPA = 0.0;
float sum = 0.0;
printHeading(outFile);
Rd(inFile, studName, studId, totalGPA);
while(inFile)
{
if((studId >= ID_LOW) && (studId <= ID_HIGH))
{
printData(outFile, studName, studId, totalGPA);
sum = sum + totalGPA;
calcMax(maxOfGPA, totalGPA);
numOfRecords++;
avgOfGPA = avg(sum, numOfRecords);
} //end While loop
inFile >> studName >> studId >> totalGPA;
}
printStatistic(outFile, maxOfGPA, avgOfGPA);
outFile << endl << "<* end *>" << endl;
inFile.close();
outFile.close();
return 0;
}
void printHeading(ofstream& outFile)
{
outFile << setw(10) << left << "Name" << setw(7) << left << "Id" << setw(7) << left << "GPA" << endl;
outFile << setw(10) << left << "----" << setw(7) << left << "--" << setw(7) << left << "---" << endl;
}
void Rd(ifstream& inFile, string& studName, int& studId, float& totalGPA)
{
inFile >> studName >> studId >> totalGPA;
}
void printData(ofstream& outFile, string studName, int studId, float GPA)
{
outFile << setw(10) << left << studName << setw(7) << left << studId << setw(7) << left << GPA << endl;
}
void calcMax(float& maxOfGPA, float totalGPA)
{
if(totalGPA > maxOfGPA)
maxOfGPA = totalGPA;
}
float avg(float sum, int numOfRecords)
{
return(sum/numOfRecords);
}
void printStatistic(ofstream& outFile, float maxOfGPA, float avgOfGPA)
{
outFile << endl << "MAX GPA: " << maxOfGPA << endl;
outFile << "AVG Of GPA: " << avgOfGPA << endl;
}
|