Lets 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() {
int num_students;
int num_tests;
cout << "Number of sudents: ";
cin >> num_students;
cout << "Number of tests: ";
cin >> num_tests;
cout << endl;
int student = 0;
do {
student++;
cout << "Enter data for the student " << student << endl;
int total = 0;
int test = 0;
do {
test++;
int score;
cout << "Test " << test << " score: ";
cin >> score;
total += score;
} while (test < num_tests);
double avg = static_cast<double>(total) / num_tests;
cout << "Average for studemt " << student << " is "
<< fixed << setprecision(2) << avg << endl << endl;
} while (student < num_students);
return 0;
}
Comments
Leave a comment