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

YOU HAVE TO SUBMIT TWO SEPARATE PROGRAMS FOR PROBLEMS 2 AND 3. Problem 2. Dynami

ID: 3826131 • Letter: Y

Question

YOU HAVE TO SUBMIT TWO SEPARATE PROGRAMS FOR PROBLEMS 2 AND 3.

Problem 2. Dynamic Array 1-D (15 points)

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 student 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.

Problem 3. Dynamic Arrays 2-D (20 points)

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.

HINT:
You need a 2-D array and hence a double-referenced pointer.
Think how you need to allocate memory to that pointer. Refer to Chapter 9 Slide 49.
For each row, you need to allocate memory as per the number of tests a student has taken. This is different from the example in Slide 49, where the number of columns was fixed for each row. You will have to ask the user for the number of columns here.
You’ll need to create another array using dynamic memory allocation to store the average for each student.

*add comments throughout programs

Explanation / Answer

Problem 2:

#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;
}

Problem 3:

#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;
}