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

Refer to what you did in Problem 2. Now the requirement of the teacher has chang

ID: 3824804 • Letter: R

Question

Refer to what you did in Problem 2. Now the requirement of the teacher has changed. Still the number of students is not known in advance while writing the program, but now she gives 5 exams in a semester. But not all students take all 5 exams-some take all 5, some take 4 and some may even take 1. For simplicity, you can assume no one takes 0 exams. Now, the program needs to compute the average grade for each student across all exams the student has taken. Compute the average by dividing the total score by 5, not how many exams the student has taken. Display the average for each student in a separate loop.

Explanation / Answer

#include <iostream>
#include <iomanip>

using namespace std;

// return average of a students grade for all test
double computeAverage(int *grades, int n, int totalTest)
{
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/totalTest;
}

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];
int *average = new int[n];
for(int i = 0; i < n; i++)
{
int m;
cout << "Enter number of test student " << (i+1) << " has taken: ";
cin >> m;
grades[i] = new int[m];
for(int j = 0; j < m; j++)
{
cout << "Enter a integer grade between 1 and 100 for test "<< (j+1) << " : ";
cin >> grades[i][j];
}
average[i] = computeAverage(grades[i], m, 5);
}
  
for(int i = 0; i < n; i++)
{
// printing average to screen.
cout << "Average grade for student "<< (i+1) << " is " << fixed << setprecision(2) << average[i] << endl;
}
for(int i = 0; i < n; ++i) {
delete [] grades[i];
}
delete [] grades;
  
// free array
delete [] average;
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote