Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Create a program that will use pointers to determine the average (to 1 decim

ID: 3593677 • Letter: C

Question

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. 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). 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. You will then use the total to compute and print out the average grade in main.

Explanation / Answer

For the mentioned problem below is the code to calculate average of grades of students using array as function argument and pointer to array :

#include <iostream>

#include <iomanip> //for std::setprecision()

using namespace std;

int getgrades(int array[]); //array declaration

int main() {

    int number;

                int gradearr[number];

                int *gradeptr = gradearr; //pointer pointing to array's 1st element

                double average , sum = 0 , count;

                number = getgrades(gradearr);

                count = number;

                while( number > 0 )

                {

                    sum = sum + *gradeptr; //calculating sum of grades

                    gradeptr++;

                    number--;

                }

                average = sum / count;     //calculating average of grades

               

                cout<<"Average grades of students is :"<<setprecision(2)<<average; //setprecision(2) for 1 decimal

}

int getgrades(int array[]) //function definition accepting array

{

    int *ptr = array , num , count = 0; //pointer ptr pointing to 1st element of array

   

    cout<<"Enter grades of students(enter -999 to stop) : "; //-999 as sentinel value

    cin>>num;

    while(num != -999){ //loop stops when user enters -999

        count++;        //counter

        *ptr = num;    

        ptr = ptr+1;    //pointer increment of address   

        cin>>num;

    }

    return count;       //total entered grades

}

Note : Bold and Italics are comments