I am having problems figuring out what I did wrong to my highest, lowest and average for grades entered by a user. I searched for help but have not found any. Any hint in what I did wrong would be greatly appreciated.
#include <iostream>
#include <iomanip>
using namespace std;
typedef int GradeType[100]; // declares a new data type:
// an integer array of 100 elements
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)
{
grades[pos] = numberOfGrades; // store grade read in to next array location
pos ++; // increment array index
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
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<< endl << "The Lowest grade is " << lowestGrade << endl;
system ("pause");
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)
{
float highest = 0; // holds the highest number
for (int pos = 0; pos < size; pos++)
{
if (array[pos] > highest)
highest = array[pos];
}
return highest;
int findLowest (const GradeType array, int size)
{ float lowest = 0;
for (int pos = 0; pos < size; pos++)
{
if (array[pos] < lowest)
lowest = array[pos];
}
return lowest;
}