Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the code: #include <iostream> using namespace std; typedef int GradeType

ID: 3568970 • Letter: H

Question

Here is the code:

#include <iostream>

using namespace std;
typedef int GradeType[100];
float findAverage(const GradeType, int);
int   findHighest(const GradeType, int);
int   findLowest(const GradeType, int);
int main()
{
   GradeType grades;  
   int numberOfGrades;
   int pos;      
   float avgOfGrades;
   int highestGrade;  
   int lowestGrade;   
   pos = 0;
   cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
   cin >> grades[pos];
   while (grades[pos] != -99)
   {
       ++pos;
       cout << " Please input a grade from 1 to 100, ( or -99 to stop)" << endl;
       cin >> grades[pos];
   }
   numberOfGrades = pos;
   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 << endl << "The lowest grade is " << lowestGrade << endl;
   system("PAUSE");
   return 0;
}
float findAverage(const GradeType array, int size)
{
float sum = 0;
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size);
}
int   findHighest(const GradeType array, int size)
{
   int highest = array[0];
   for (int i = 0; i < size; i++)
   {
       if (array[i] > highest)
           highest = array[i];
   }
   return highest;
}
int   findLowest(const GradeType array, int size)
{
   int lowest = array[0];
   for (int i = 0; i < size; i++)
   {
       if (array[i] < lowest)
           lowest = array[i];
   }
   return lowest;
}


What I need to do:

Modify my program so that it reads the information from the gradfile.txt file, reading until the end of file is encountered. gradfile has the following values on seperate lines: 90 45 73 21 62

I gave it a shot, but my code keeps freezing. I was hoping someone could explain why and provide the correct code.

Here is my try:

#include <iostream>
#include <fstream>
using namespace std;
typedef int GradeType[100];
float findAverage(const GradeType, int);
int   findHighest(const GradeType, int);
int   findLowest(const GradeType, int);
int main()
{
   GradeType grades;
   int numberOfGrades;
   int pos;
   float avgOfGrades;
   int highestGrade;
   int lowestGrade;
   pos = 0;
   ifstream input;
   input.open("gradfile.txt");
   input >> grades[pos];
   while (grades[pos] != -99)
   {
       ++pos;
       input >> grades[pos];
   }
   numberOfGrades = pos;
   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 << endl << "The lowest grade is " << lowestGrade << endl;
   input.close();
   return 0;
}
float findAverage(const GradeType array, int size)
{
   float sum = 0;
   for (int pos = 0; pos < size; pos++)
       sum = sum + array[pos];
   return (sum / size);
}
int   findHighest(const GradeType array, int size)
{
   int highest = array[0];
   for (int i = 0; i < size; i++)
   {
       if (array[i] > highest)
           highest = array[i];
   }
   return highest;
}
int   findLowest(const GradeType array, int size)
{
   int lowest = array[0];
   for (int i = 0; i < size; i++)
   {
       if (array[i] < lowest)
           lowest = array[i];
   }
   return lowest;
}

Explanation / Answer

You can check for 'eof' bit in ifstream class to know the whether we reached the end of the file or not.

You have been checking for -99 as input for the eof, but when we reach the end of file it is not always guaranteed that the eof is read as -99. It can be any negative number depending on the implementation. Its better to check for the 'eof' bit instead of relying on the negative number being read.

////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
using namespace std;
typedef int GradeType[100];
float findAverage(const GradeType, int);
int findHighest(const GradeType, int);
int findLowest(const GradeType, int);
int main()
{
GradeType grades;
int numberOfGrades;
int pos;
float avgOfGrades;
int highestGrade;
int lowestGrade;
pos = 0;
ifstream input;
input.open("gradfile.txt");
while (true)
{
input >> grades[pos];
   pos++;
   if( input.eof() )
           break;
}
numberOfGrades = pos;
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 << endl << "The lowest grade is " << lowestGrade << endl;
input.close();
return 0;
}
float findAverage(const GradeType array, int size)
{
float sum = 0;
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size);
}
int findHighest(const GradeType array, int size)
{
int highest = array[0];
for (int i = 0; i < size; i++)
{
if (array[i] > highest)
highest = array[i];
}
return highest;
}
int findLowest(const GradeType array, int size)
{
int lowest = array[0];
for (int i = 0; i < size; i++)
{
if (array[i] < lowest)
lowest = array[i];
}
return lowest;
}

////////////////////////////////////////////////////////////////////////////////