I am having trouble getting the getAverage function to get the correct average. What am I doing wrong? I cannot use pointers. Here is the original question:
A program that will prompt for grades and calculate the average. The grades will be stored in an array called GradesInput that is defined in main. The maximum number of grades that can be stored in the array is 100. The variable holding the actual number of grades the user entered should be defined in main and should be called GradeCount. The program will have two functions in addition to main. The 1st function should be called from main and should keep prompting the user for grades till a sentinel is entered, capture those grades and store them in GradesInput. This function should also update the variable GradeCount in main, GradeCount should have been passed by reference. The 2nd function should be called from main and should find the average of the grades in GradesInput. The average should be returned to and printed out from main.
//Lab7D This program will get the grades of students and average
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//The 1st function should be called from main and should keep prompting the user for grades till a sentinel is entered,
//capture those grades and store them in GradesInput.
//This function should also update the variable GradeCount in main, GradeCount should have been passed by reference.
void store(int arr[], int &GradeCount) //for taking input by user pass by reference
{
int i = 0;
while (arr[i] != -1)
{
cout << "Enter grade : ";
cin >> arr[i];
GradeCount++;
}
}
//The 2nd function should be called from main and should find the average of the grades in GradesInput.
//he average should be returned to and printed out from main.
double getAverage(int arr[], int size)
{
int i;
double avg, sum = 0;
for (i = 0; i < size; i++)
{
sum += arr[i];
}
avg = sum / size;
return avg;
}
//The variable holding the actual number of grades the user entered should be defined in main and should be called GradeCount
int main()
{
int GradeCount = 0;
int grades[100];
store(grades, GradeCount);
cout << "Average : " << getAverage(grades, GradeCount) << endl;
}