//****************************************************************** // Statistics Program // This program calculates the average, high score, low score, // number above the average, and number below the average for // a file of test scores. The SortedList ADT is used // Assumption: File "testScores" is not empty and does not contain // more than MAX_GRADES values. // To save space, we omit from each function the precondition // comments that document the assumptions made about valid input // parameter data. These would be included in a program intended // for actual use. //****************************************************************** #include #include #include #include "SortedList.h" using namespace std; // Function Prototypes void OpenFiles(ifstream& inData, ofstream& outData); void InputGrades(SortedList& grades, ifstream& inData); float CalculateAverage(SortedList grades); int CalculateHighest(SortedList grades); int CalculateLowest(SortedList grades); int CalculateAboveAverage(SortedList grades, float average); int CalculateBelowAverage(SortedList grades, float average); void PrintResults(ofstream& outData, SortedList grades, float average, int highest, int lowest, int aboveAverage, int belowAverage); int main() { SortedList grades; // A SortedList of grades float average; // Average grade int highest; // Highest grade int lowest; // Lowest grade int aboveAverage; // Number of grades above the average int belowAverage; // Number of grades below the average // Declare and open files ifstream inData; ofstream outData; OpenFiles(inData, outData); if ( !inData || !outData ) { cout << "Files did not open successfully." << endl; return 1; } // Process grades InputGrades(grades, inData); average = CalculateAverage(grades); highest = CalculateHighest(grades); lowest = CalculateLowest(grades); aboveAverage = CalculateAboveAverage(grades, average); belowAverage = CalculateBelowAverage(grades, average); PrintResults(outData,grades, average, highest, lowest, aboveAverage, belowAverage); inData.close(); outData.close(); return 0; } //***************************************************************** void OpenFiles( /* inout */ ifstream& text, /* inout */ ofstream& outFile ) // Function OpenFiles reads in the names of the input file and // the output file and opens them for processing // Postcondition: // Files have been opened AND a label has been written on the // output file { string inFileName; string outDataName; cout << "Enter the name of the file to be processed" << endl; cin >> inFileName; text.open(inFileName.c_str()); cout << "Enter the name of the output file" << endl; cin >> outDataName; outFile.open(outDataName.c_str()); // Write label on output outFile << "Grade statistics using the SortedList ADT" << endl << endl; } //****************************************************************** void InputGrades( /* inout */ SortedList& grades, // Grade SortedList /* inout */ ifstream& inData ) // Input file // Grades are input from file inData and inserted into grades. // Precondition: // File is not empty // Postcondition: // Each grade in the file has been inserted into SortedList of grades { int grade; // Read grades and put them into the SortedList inData >> grade; while (inData && !grades.IsFull()) { grades.Insert(grade); inData >> grade; } } //***************************************************************** float CalculateAverage( /* in */ SortedList grades ) // This function calculates the average test score // Postcondition: // Return value is the average grade { int sum = 0; int limit = grades.Length(); // limit is the number of grades int grade; grades.Reset(); // Prepare for traversal // Add each grade to sum for (int index = 0; index < limit; index++) { grade = grades.GetNextItem(); sum = sum + grade; } return float(sum) / float(limit); // Return average } //****************************************************************** int CalculateHighest( /* in */ SortedList grades ) // SortedList of grades // This function calculates the highest grade in the SortedList of // grades // Postcondition: // Return value is the highest grade { int limit = grades.Length(); // Number of grades int grade; grades.Reset(); // Prepare for iteration int maxGrade = 0; // Find the maximum grade in the SortedList for (int index = 0; index < limit; index++) { grade = grades.GetNextItem(); if (grade > maxGrade) maxGrade = grade; } return maxGrade; } //***************************************************************** int CalculateLowest( /* in */ SortedList grades ) // SortedList of grades // This function calculates the lowest grade in the SortedList of // grades // Postcondition: // Return value is the lowest grade { int limit = grades.Length(); // Number of grades int grade; grades.Reset(); // Prepare for iteration int minGrade = 100; // Find the miminum grade in the SortedList for (int index = 0; index < limit; index++) { grade = grades.GetNextItem(); if (grade < minGrade) minGrade = grade; } return minGrade; } //***************************************************************** int CalculateAboveAverage ( /* in */ SortedList grades, // SortedList of grades /* inout */ float average ) // Average grade // This function calculates the number of grades above the // average // Postcondition: // Return value is the number of grades above average { int roundedAverage = (int) (average + 0.5); int limit = grades.Length(); // Number of grades int grade; int number = 0; grades.Reset(); // Prepare for iteration // Calculate the number of grades above the average for (int index = 0; index < limit; index++) { grade = grades.GetNextItem(); if (grade > roundedAverage) number++; } return number; } //***************************************************************** int CalculateBelowAverage ( /* in */ SortedList grades, // SortedList of grades /* inout */ float average ) // Average grade // This function calculates the number of grades below the // average // Postcondition: // Return value is the number of grades below average { int truncatedAverage = (int) (average); int limit = grades.Length(); // Number of grades int grade; int number = 0; grades.Reset(); // Prepare for iteration // Calculate the number of grades below the average for (int index = 0; index < limit; index++) { grade = grades.GetNextItem(); if (grade < truncatedAverage) number++; } return number; } //***************************************************************** void PrintResults( /* inout */ ofstream& outData, // Output file /* in */ SortedList grades, // Grade SortedList /* in */ float average, // Average /* in */ int highest, // Max grade /* in */ int lowest, // Min grade /* in */ int aboveAverage, // Number above /* in */ int belowAverage ) // Number below // Statistics are printed on file outData // Precondition: // Output file has been successfully opened // Postcondition: // Statistics have been written on outData, appropriately // labeled { outData << "The number of grades is " << grades.Length() << endl; outData << fixed << setprecision(2) << "The average grade is " << average << endl; outData << "The highest grade is " << highest << endl; outData << "The lowest grade is " << lowest << endl; outData << "The number of grades above the average is " << aboveAverage << endl; outData << "The number of grades below the average is " << belowAverage << endl; }