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 sums and averages of each row of scores stored in the 2-dimensional array: students so that when the program executes, the following output is displayed.
Average scores for students:
Student # 1: 12
Student # 2: 22
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}
};
double averages[NUM_OF_STUDENTS] = {0, 0};
// Write your code here:
cout << "Average scores for students: " << endl;
for (int i = 0; i < NUM_OF_STUDENTS; i++)
cout << "Student # " << i + 1 << ": " << averages[i] << 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}
};
double averages[NUM_OF_STUDENTS] = {0, 0};
// Write your code here:
cout << "Sum scores for students: " << endl;
for(int i = 0; i < NUM_OF_STUDENTS; i ++){
for(int j = 0 ; j < NUM_OF_TESTS; j ++){
sum += students[i][j];
}
averages[i] = sum / NUM_OF_TESTS;
cout << "Student # " << i + 1 << ": " << sum << endl;
sum = 0;
}
cout << endl;
// End my code
cout << "Average scores for students: " << endl;
for (int i = 0; i < NUM_OF_STUDENTS; i++)
cout << "Student # " << i + 1 << ": " << averages[i] << endl;
return 0;
}
Comments
Leave a comment