44 Create a program that will use pointers to determine the
4-4. Create a program that will use pointers to determine the average (to 1 decimal place) of a series of grades entered into an array. You can assume a maximum of 15 students and the user will end input with a sentinel value.
You will use a function called getgrades. Send the array name (a pointer constant) to the function and accept it as a pointer variable (you can now use the pointer to increment through the array). Hint: 4-3 increments a pointer through an array. Using the pointer, keep accepting grades to fill up the array until they enter a sentinel value. Input into where the pointer is pointing. When the sentinel is entered, you will only return the number of grades entered.
Back in main, by setting a grade pointer to the beginning of the grades array, you will use the pointer to increment through each grade (only the grades inputted) to compute the total by using a pointer comparison. You will then use the total to compute and print out the average grade in main.
Solution
Dear Asker,
Following is the C++ implementation of the problem:
#include <iostream>
using namespace std;
int getGrades(int* gradArray)
{
int inputGrade;
int count=0;
cin>>inputGrade;
while(inputGrade!=-1)
{
count++;
*gradArray=inputGrade;
gradArray++;
cin>>inputGrade;
}
return count;
}
int main()
{
int inputGradeArray[15];
int numStudents = getGrades(inputGradeArray);
float gradeAverage = 0;
for (int* initalPointer = inputGradeArray; initalPointer!=inputGradeArray+numStudents; initalPointer++){
gradeAverage+= *initalPointer;
}
gradeAverage/=numStudents;
cout<<\"The average grade is :\"<<gradeAverage;
}
