C++
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. Make sure to use pointer increment and a pointer comparison while loop, and not array notation or array index numbers or offsets.
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 - without having to move over index number spots). Using the pointer, keep accepting grades to fill up the array until they enter a sentinel value, so use a sentinel while loop. 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 while loop. You will then use the total to compute and print out the average grade in main. Attach your .cpp file and an output .txt file and submit.
// I HOPE I COULD HELP :)
//HERE IS THE CODE which can be copied and saved as .cpp file
#include<iostream>
#include<iomanip>
using namespace std;
int getgrades(int *p)
{
int input=-1;
int no_of_grades_entered=0;
cout<<"Input the grades of students Press
0 to stop taking input"<<endl;
//program will stop taking input once user
enters 0
while(input!=0)
{
cin>>input;
*p=input;
p=p+1;
if(input!=0)
no_of_grades_entered++;
}
//setting the end point
*p=0;
return no_of_grades_entered;
}
int main()
{
int arr[15];
int *p=arr;
float sum=0.0;
int no_of_grades_entered=getgrades(p);
int *grades_pointer=arr;
while((*grades_pointer)!=0)
{
sum+=*grades_pointer;
grades_pointer+=1;
}
float
average=sum*1.0/no_of_grades_entered;
//setprecision(2) has been used to print average
upto 1 decimal place
cout<<"Average Grade is:
"<<setprecision(2)<<average;
}
OUTPUT
Input the grades of students Press 0 to stop taking input
1 2 3 4 5 6 0
Average Grade is: 3.5
Get Answers For Free
Most questions answered within 1 hours.