Les assume you create a system which asks the user for the number of students and the
number of test scores per student. A nested inner loop, should ask for all the test scores for one
student, iterating once for each test score. The outer loop should iterate once for each student
and take student name as input. After taking input for 3 test scores, calculate average for each
student. Take 3 test scores and 4 students.
Note: use do-while loops
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int NUM_STUDENTS = 4;
const int NUM_TESTS = 3;
int num_student = 1;
do {
cout << "Enter data for the " << num_student << "th student" << endl;
int sum = 0;
int num_test = 1;
do {
int score;
cout << "Test " << num_test << ": ";
cin >> score;
sum += score;
num_test++;
} while (num_test <= NUM_TESTS);
double avg = sum / NUM_TESTS;
cout << "Average score is "
<< fixed << setprecision(2) << avg << endl << endl;
num_student++;
} while (num_student <= NUM_STUDENTS);
return 0;
}
Comments
Leave a comment