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

#include #include #include #include #include #include Dynamic Array 1 - D A teac

ID: 3824803 • Letter: #

Question

#include #include #include #include #include #include Dynamic Array 1 - D A teacher needs a program to grade her students. She wants to make the program flexible so she can use it every year, so the number of students is not fixed. The program should ask the teacher for the number of students and then using DYNAMIC MEMORY ALLOCATION, create the dynamic array for the grade of the students. The program then needs to ask the teacher the integer grade for each student in the range of 1 to 100, and store it in a dynamic array. After reading completely, pass your dynamic array to a programmer defined function that returns the average grade across the class. Display the average grade of the class.

Explanation / Answer

#include <iostream>
#include <iomanip>

using namespace std;

// return average of grade array given
double computeAverage(int *grades, int n)
{
double total = 0;
  
// sum of int grades to compute average;
for(int i = 0; i < n; i++)
{
total += grades[i];
}
  
// computing average and returning
return total/n;
}

int main()
{
int n;
// take number of student as input from user
cout << "Enter number of student: ";
cin >> n;
  
// create an array dynamically.
int *grades = new int[n];
  
for(int i = 0; i < n; i++)
{
cout << "Enter a integer grade between 1 and 100: ";
cin >> grades[i];
}
  
// printing average to screen.
cout << "Average grade of class is " << fixed << setprecision(2) << computeAverage(grades, n) << endl;

// free array
delete [] grades;

return 0;
}