In: Computer Science
Implement a version with the outer loop, with a while loop, and the inner loop with a do / while loop.
Modify this program as he ask ↑↑↑ C++
// This program averages test scores. It asks the user for
the
2 // number of students and the number of test scores per
student.
3 #include <iostream>
4 #include <iomanip>
5 using namespace std;
6
7 int main()
8 {
9 int numStudents, // Number of students
10 numTests; // Number of tests per student
11 double total, // Accumulator for total scores
12 average; // Average test score
13
14 // Set up numeric output formatting.
15 cout << fixed << showpoint <<
setprecision(1);
16
17 // Get the number of students.
18 cout << "This program averages test scores.\n";
19 cout << "For how many students do you have scores?
";
20 cin >> numStudents;
21
22 // Get the number of test scores per student.
23 cout << "How many test scores does each student have?
";
24 cin >> numTests;
25
26 // Determine each student's average score.
27 for (int student = 1; student <= numStudents;
student++)
28 {
29 total = 0; // Initialize the accumulator.
30 for (int test = 1; test <= numTests; test++)
31 {
32 double score;
33 cout << "Enter score " << test << " for
";
34 cout << "student " << student << ": ";
35 cin >> score;
36 total += score;
37 }
38 average = total / numTests;
39 cout << "The average score for student " <<
student;
40 cout << " is " << average << ".\n\n";
41 }
42 return 0;
43 }
Ans
code:-
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numStudents,
numTests; // Number of tests per student
double total, //Accumulator for total scores
average; // Average test score
// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(1);
// Get the number of students.
cout << "This program averages test scores.\n";
cout << "For how many students do you have scores? ";
cin >> numStudents;
// Get the number of test scores per student.
cout << "How many test scores does each student have? ";
cin>>numTests;
// Determine each student's average score.
int student=1;
while (student <= numStudents)
{
total = 0; // Initialize the accumulator.
int test=1;
do
{
double score;
cout << "Enter score " << test << " for ";
cout << "student " << student << ": ";
cin >> score;
total += score;
test++;
}
while(test<=numTests);
average = total / numTests;
cout << "The average score for student " << student;
cout << " is " << average << ".\n\n";
student++;
}
return 0;
}
If any doubt ask in the comments.
Please appreciate the work by giving a thumbs up.