Answer to Question #340232 in C++ for CMC

Question #340232

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.

OUTPUT:

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;

}


1
Expert's answer
2022-05-12T08:20:32-0400
#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:
   	double sums[NUM_OF_STUDENTS] = {0, 0};
   	
   	for(int i = 0; i < NUM_OF_STUDENTS; i ++){
		for(int j = 0 ; j < NUM_OF_TESTS; j ++){
			sum += students[i][j];
		}
		sums[i] = sum;
		averages[i] = sums[i] / NUM_OF_TESTS;
		sum = 0;
	}
   	cout << "Sum scores for students: " << endl;
	for (int i = 0; i < NUM_OF_STUDENTS; i++)
		cout << "Student # " << i + 1 << ": " << sums[i] << endl;
	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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS