Question

In: Computer Science

Calculate average test score: Write a C++ program that calls a function to pass array of...

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

Solutions

Expert Solution

Short Summary:

  • Used pointers of pointer for 2D array.
  • Created two different fucntions.
    • calculateAvg - accepts two 2d array and returns double array which has average of test score passed.
    • diaplayScoresAndAvg - displays testscores and average in a neat table format.

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!!!

******************************************************************************


Related Solutions

Write program that pass unsorted one dimensional array and a key to function, the function will...
Write program that pass unsorted one dimensional array and a key to function, the function will search for the key(using linear search) and if it is found the function will sort the array from the beginning to the position of the key, and f it is not found the function will sort all the array Note: please solve it by c , not c++
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
In C++. Write a program that uses array to calculate the factorial of a reasonable large...
In C++. Write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).  
Write a program that calls a/few function(s) to determine the smaller of two arguments. Test the...
Write a program that calls a/few function(s) to determine the smaller of two arguments. Test the program using integer, character and floating-point number arguments. Produce 3 separate programs solving the problem above. The three approaches you should be using are: a) 3 different functions (findMinimum1(), findMinimum2(), findMinimum3()), each to solve different data types parameters. b) Function overloading with only one function named findMinimum( ) c) Function template with only one function named findMinimum( )
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
OBJECTIVE-C For this program a teacher needs to be able to calculate an average of test...
OBJECTIVE-C For this program a teacher needs to be able to calculate an average of test scores for students in their course. Your program must ask the Professor ho many students and how many tests will be averaged per student. Your program should then allow the Professor to enter scores for each student one at a time and then average those scores together. For example, if a student has 5 scores to be entered and the scores are 80, 60,...
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT