MAX_LENGTH in the specification file to be at least 100.
1. There is a personal preference.
2.
//****************************************************************** // 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 List 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.
// CalculateAverage, CalculateHighest, and CalculateLowest are // combined into one function.
//****************************************************************** #include <fstream>
#include <iostream> #include <iomanip> #include "list.h" using namespace std; // Function Prototypes
void OpenFiles(ifstream& inData, ofstream& outData); void InputGrades(List& grades, ifstream& inData);
void Calculate(List grades, float& average, int& highest, int& lowest);
int CalculateAboveAverage(List grades, float average); int CalculateBelowAverage(List grades, float average);
void PrintResults(ofstream& outData, List grades, float average, int highest, int lowest, int aboveAverage, int belowAverage); int main()
{
List grades; // A list 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 // i.outData ) {
cout << "Files did not open successfully" << endl;
return 1; }
// Process grades
InputGrades(grades, inData);
Calculate(grades, average, highest, lowest);
aboveAverage = CalculateAboveAverage(grades, average); belowAverage = CalculateBelowAverage(grades, average); PrintResults(outData,grades, average, highest, lowest,
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 List ADT" << endl << endl;
}
//****************************************************************** void InputGrades( /* inout */ List& grades, // Grade list
/* inout */ ifstream& inData ) // Input file // Grades are input from file inData and inserted into grades. // Precondition:
// File is not empty // Postcondition:
{
int grade;
// Read grades and put them into the list inData >> grade;
while (inData && !grades.IsFull()) { grades.Insert(grade); inData >> grade; } } //***************************************************************** void Calculate( /* in */ List grades,
/* out */ float& average, /* out */ int& highest, /* out */ int& lowest )
// 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;
highest = 0; lowest = 100;
grades.Reset(); // Prepare for traversal // Add each grade to sum
for (int index = 0; index < limit; index++) {
grade = grades.GetNextItem(); sum = sum + grade;
if (grade > highest) highest = grade; if (grade < lowest)
lowest = grade; }
average = float(sum) / float(limit); // Return average }
//***************************************************************** int CalculateAboveAverage
( /* in */ List grades, // List 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 */ List grades, // List 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 */ List grades, // Grade list /* 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;
3.
//****************************************************************** // 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 List 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.
// CalculateAboveAverage and CalculateBelowAverage are combined // into one function.
//****************************************************************** #include <fstream> #include <iostream> #include <iomanip> #include "list.h" using namespace std; // Function Prototypes
void OpenFiles(ifstream& inData, ofstream& outData); void InputGrades(List& grades, ifstream& inData);
void Calculate(List grades, float& average, int& highest, int& lowest);
void CalculateAboveBelow(List grades, float average, int& aboveAverage, int& belowAverage);
void PrintResults(ofstream& outData, List grades, float average, int highest, int lowest, int aboveAverage, int belowAverage); int main()
{
List grades; // A list 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 // i.outData ) {
cout << "Files did not open successfully" << endl;
return 1; }
// Process grades
InputGrades(grades, inData);
Calculate(grades, average, highest, lowest);
CalculateAboveBelow(grades, average, aboveAverage, belowAverage); 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 List ADT" << endl << endl;
}
//****************************************************************** void InputGrades( /* inout */ List& grades, // Grade list
/* 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 list of grades {
int grade;
// Read grades and put them into the list inData >> grade;
while (inData && !grades.IsFull()) { grades.Insert(grade); inData >> grade; } } //***************************************************************** void Calculate( /* in */ List grades,
/* out */ float& average, /* out */ int& highest, /* out */ int& lowest )
// 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;
highest = 0; lowest = 100;
grades.Reset(); // Prepare for traversal // Add each grade to sum
for (int index = 0; index < limit; index++) {
grade = grades.GetNextItem(); sum = sum + grade;
if (grade > highest) highest = grade; if (grade < lowest)
lowest = grade; }
average = float(sum) / float(limit); // Return average }
//***************************************************************** void CalculateAboveBelow
( /* in */ List grades, // List of grades /* in */ float average, // Average grade /* out */ int& aboveAverage,
/* out */ int& belowAverage)
// 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 truncatedAverage = (int) (average);
int limit = grades.Length(); // Number of grades int grade;
aboveAverage = 0 ; belowAverage = 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) aboveAverage++; if (grade < truncatedAverage) belowAverage++; } } //***************************************************************** int CalculateBelowAverage
( /* in */ List grades, // List 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 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(); } return number; } //***************************************************************** void PrintResults( /* inout */ ofstream& outData, // Output file
/* in */ List grades, // Grade list /* 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;
}