In: Computer Science
C++
Text file contains numbers 92 87 65 49 92 100 100 100 82 75 64 55 100 98 -99
Modify your program from Exercise 1 so that it reads the information from the gradfile.txt file, reading until the end of file is encountered. You will need to first retrieve this file from the Lab 7 folder and place it in the same folder as your C++ source code. Run the program
#include <iostream>
using namespace std;
typedef int GradeType[100]; // declares a new data type:
float findAverage (const GradeType, int); // finds average of all
grades
int findHighest (const GradeType, int); // finds highest of all
grades
int findLowest (const GradeType, int); // finds lowest of all
grades
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
// Read in the values into the array
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to
stop)" << endl;
cin >> grades[pos];
while (grades[pos] != -99)
{
pos++;
cin >> grades[pos];
}
numberOfGrades = pos--;
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is "
<< avgOfGrades << endl;
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " <<
highestGrade << endl;
lowestGrade = findLowest(grades, numberOfGrades);
cout << "The lowest grade is " << lowestGrade <<
endl;
return 0;
}
float findAverage (const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}
int findHighest (const GradeType array, int size)
{
int highest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( highest < array[pos])
{
highest = array[pos];
}
}
return highest;
}
int findLowest (const GradeType array, int size)
{
int lowest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( lowest > array[pos])
{
lowest = array[pos];
}
}
return lowest;
}
Updated C++ Program:
#include <iostream>
#include <fstream>
using namespace std;
typedef int GradeType[100]; // declares a new data type:
float findAverage (const GradeType, int); // finds average of all
grades
int findHighest (const GradeType, int); // finds highest of all
grades
int findLowest (const GradeType, int); // finds lowest of all
grades
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
//Opening file for reading
fstream fin("gradfile.txt", ios::in);
// Read in the values into the array
pos = 0;
//Reading first grade
fin >> grades[pos];
//Loop till all the grades are read
while(grades[pos] != -99)
{
pos++;
//Reading from file
fin >> grades[pos];
}
//Closing file
fin.close();
numberOfGrades = pos--;
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " <<
highestGrade << endl;
lowestGrade = findLowest(grades, numberOfGrades);
cout << "The lowest grade is " << lowestGrade <<
endl;
return 0;
}
float findAverage (const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}
int findHighest (const GradeType array, int size)
{
int highest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( highest < array[pos])
{
highest = array[pos];
}
}
return highest;
}
int findLowest (const GradeType array, int size)
{
int lowest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( lowest > array[pos])
{
lowest = array[pos];
}
}
return lowest;
}
___________________________________________________________________________________________
Sample Run: