In: Computer Science
Calculate average test score:
Write a C++ program that calls a function to pass array of test scores of students and calculates the average test scores for each student. Let the user enters 2 students’ test scores for three classes (use two dimensional array). Write another function to pass array of test scores and average to display the test scores for each student and display the average of the test scores for each student as well. So you need to write two functions (one for function to pass array of test scores and average to display the test scores for each student and another for display the average of the test scores for each student as well. ) . You cannot use global variable.
Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
Test scores should be between 0-100 (data Validation).
Short Summary:
Source Code:
#include <iostream>
#include <iomanip> // std::setw
using namespace std;
double* calculateAvg(int**, int, int);
void diaplayScoresAndAvg(int **, double *, int, int);
int main()
{
const int totalStudents = 2;
const int totalClasses = 3;
//Let the user enters 2 students’ test scores for three classes
(use two dimensional array).
int **scores;
scores = new int*[totalStudents]; // dynamic array (size 10) of
pointers to int
// output each array element's value
int score;
for (int studentNo = 0; studentNo < totalStudents; studentNo++)
{
cout << "Enter Scores for Student " << studentNo + 1
<< ": " << endl;
scores[studentNo] = new int[totalClasses];
for (int classNo = 0; classNo < totalClasses; classNo++) {
// continue to get score, until it is between (0 - 100)
do{
cout << "Scores of Class " << classNo + 1 << ":
";
cin >> score;
cin.ignore();
if(score <=0 || score > 100){
cout << "Invalid score" << endl;
}
}while(score <=0 || score > 100);
scores[studentNo][classNo] = score;
}
}
double* averages;
averages = calculateAvg(scores, totalStudents, totalClasses);
diaplayScoresAndAvg(scores, averages, totalStudents,
totalClasses);
return 0;
}
//function to pass array of test scores of students
//and calculates the average test scores for each student.
double* calculateAvg(int **testScores, int totalStudents, int
totalClasses){
// Create a result array size of total students
double* average = new double[totalStudents];
// Loop through the 2D array and calculate sum and its
average
double sum;
for (int studentNo = 0; studentNo < totalStudents; studentNo++)
{
// Reset sum for each iteration
sum = 0;
for (int classNo = 0; classNo < totalClasses; classNo++) {
sum += testScores[studentNo][classNo];
}
// store the average in the result array
average[studentNo] = sum / totalClasses;
}
// return the resulted average array
return average;
}
//pass array of test scores and average to display the test
scores for each student and
//display the average of the test scores for each student as
well.
void diaplayScoresAndAvg(int **testScores, double *avg, int
totalStudents, int totalClasses){
// display heading
cout << endl << "Scores and Average:" <<
endl;
for (int classNo = 0; classNo < totalClasses; classNo++) {
cout << setw(10) << "Class " << (classNo +
1);
}
cout << setw(10) << "Average" << endl;
cout << fixed << setprecision(2);
//display score and Average
for (int studentNo = 0; studentNo < totalStudents; studentNo++)
{
for (int classNo = 0; classNo < totalClasses; classNo++) {
cout << setw(10) <<
testScores[studentNo][classNo];
}
cout << setw(10) << avg[studentNo] << endl;
}
}
Refer the following screenshots for code indentation:


Sample Run:

******************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
******************************************************************************