When compiled, this program gives:
The number of grades is 75
The average is: 68.92
The highest grade is 100
The lowest grade is 0
The number of grades above the average is 38
The number of grades below the average is 37
However, the lowest grade should be 11. I can change line 87 in function CalculateHighest in such a way that instead of 'numGrades' in the for loop, it will be a random number less than 75. And the 'highest' number returned is correct. However, for 'lowest', it will always return 0. I was wondering if anyone might be able to point out where my code has gone wrong.
The txt file in question contains:
28
48
67
84
91
55
88
99
32
11
68
95
55
66
77
88
99
100
99
98
77
42
88
87
95
55
88
99
32
12
23
45
55
66
77
88
99
100
99
98
37
44
61
87
93
55
88
99
32
12
23
45
55
66
77
88
99
100
99
98
44
49
66
84
79
55
88
99
32
57
42
45
55
66
77
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
int numGrades; // Number 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
int grades[101]; // Array of grades
void CalculateStatistics(ifstream& inData);
void OpenFiles(ifstream& inData, ofstream& outData);
void InputGrades(ifstream&);
void CalculateAverage(ifstream& inData);
void CalculateHighest(ifstream& inData);
void CalculateLowest(ifstream& inData);
void CalculateAboveAverage(ifstream& inData);
void CalculateBelowAverage(ifstream& inData);
void PrintResults(ofstream& outData);
int main()
{
ifstream indata;
ofstream outdata;
OpenFiles(indata, outdata);
CalculateStatistics(indata);
PrintResults(outdata);
indata.close();
outdata.close();
return 0;
}
void CalculateStatistics(ifstream& indat)
{
InputGrades(indat);
CalculateAverage(indat);
CalculateHighest(indat);
CalculateLowest(indat);
CalculateAboveAverage(indat);
CalculateBelowAverage(indat);
}
void OpenFiles(ifstream& indat, ofstream& outdat)
{
indat.open("C:/Users/Sean/Desktop/pocketcpp/A5Q1.txt");
if(!indat)
{
cout << "File was not opened." << endl;
}
outdat.open("C:/Users/Sean/Desktop/pocketcpp/A5durr.txt");
if (!outdat)
{
cout << "Didn't work!" << endl;
}
}
void InputGrades(ifstream& indat)
{
numGrades = 75;
for (int i = 0; i < numGrades; i++)
indat >> grades[i];
cout << "The number of grades is " << numGrades ;
}
void CalculateAverage(ifstream& indat)
{
float sum = 0;
numGrades = 75;
for (int i = 0; i < numGrades; i++)
sum += grades[i];
average = sum/numGrades;
cout << endl << setprecision(4) << "The average is: " << average << endl;
}
void CalculateHighest(ifstream& indat)
{
for(int i = 0; i < numGrades; i++)
{
if (highest < grades[i])
highest = grades[i];
}
cout << "The highest grade is " << highest << endl;
}
void CalculateLowest(ifstream& indat) //Where my trouble is
{
for (int i = 0; i < numGrades; i++)
{
if (lowest > grades[i])
lowest = grades[i];
}
cout << "The lowest grade is " << lowest << endl;
}
void CalculateAboveAverage(ifstream& indat)
{
for (int i = 0; i < numGrades; i++)
{
if (average < grades[i])
aboveAverage++;
}
cout << "The number of grades above the average is " << aboveAverage << endl;
}
void CalculateBelowAverage(ifstream& indat)
{
for (int i = 0; i < numGrades; i++)
{
if (average > grades[i])
belowAverage++;
}
cout << "The number of grades below the average is " << belowAverage << endl;
}
void PrintResults(ofstream& outdat)
{
outdat << "The number of grades is " << numGrades ;
outdat << endl << setprecision(4) << "The average is: " << average << endl;
outdat << "The highest grade is " << highest << endl;
outdat << "The lowest grade is " << lowest << endl;
outdat << "The number of grades above the average is " << aboveAverage << endl;
outdat << "The number of grades below the average is " << belowAverage << endl;
}
void CalculateLowest(ifstream& indat) //Where my trouble is
{
lowest = grades[0]; //<<-------------------------------add this
for (int i = 0; i < numGrades; i++)
{
if (lowest > grades[i])
lowest = grades[i];
}
cout << "The lowest grade is " << lowest << endl;
}