Examine the incomplete program below. Write code that can be placed below the comment (// Write your code here) to complete the program. Use nested loops to calculate the sum and average of all scores stored in the 2-dimensional array: students so that when the program executes, the following output is displayed. Do not change any other part of the code.
OUTPUT:
Sum of all scores = 102
Average score = 17
CODE:
#include <iostream>
using namespace std;
int main()
{
const int NUM_OF_STUDENTS = 2;
const int NUM_OF_TESTS = 3;
int sum = 0, average = 0;
double students[NUM_OF_STUDENTS][NUM_OF_TESTS] =
{
{11, 12, 13},
{21, 22, 23}
};
// Write your code here:
cout << "Sum of all scores = " << sum << endl;
cout << "Average score = " << average << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int NUM_OF_STUDENTS = 2;
const int NUM_OF_TESTS = 3;
int sum = 0, average = 0;
double students[NUM_OF_STUDENTS][NUM_OF_TESTS] =
{
{11, 12, 13},
{21, 22, 23}
};
// Write your code here:
int i,j;
for( i=0; i<NUM_OF_STUDENTS; ++i) {
for( j=0; j<NUM_OF_TESTS; ++j)
sum = sum + students[i][j];
}
average = sum/(NUM_OF_STUDENTS*NUM_OF_TESTS);
cout << "Sum of all scores = " << sum << endl;
cout << "Average score = " << average << endl;
return 0;
}
Comments
Leave a comment