In: Computer Science
Question: We have 10 students that each of them passed 3 tests. Write a c++ program that asks each student to enter 3 grades for each test and then find the average by using nested loop. Validate each grade. (before getting the grade test it whether it is greater than 0 and less than 100).
#include <iostream>
using namespace std;
int main()
{
//variable declaration
int test[3], grade;
float avg[10];
//get user input
for(int i=0; i<10; i++)
{
cout<<"Enter the grade of student "<<(i+1)<<":
"<<endl;
for(int k=0; k<3; k++)
{
cout<<"Test "<<(k+1)<<": ";
cin>>grade;
if(grade>=0 && grade<=100)
{
test[k] = grade;
}
}
avg[i] = (test[0]+test[1]+test[2]) / 3.0;
}
//display the average grade
cout<<endl<<"The average grades are given below:
"<<endl;
for(int i=0; i<10; i++)
{
cout<<"Average grade of student "<<(i+1)<<" =
"<<avg[i]<<endl;
}
return 0;
}
OUTPUT:
Enter the grade of student 1:
Test 1: 40
Test 2: 50
Test 3: 60
Enter the grade of student 2:
Test 1: 50
Test 2: 60
Test 3: 70
Enter the grade of student 3:
Test 1: 80
Test 2: 60
Test 3: 50
Enter the grade of student 4:
Test 1: 40
Test 2: 30
Test 3: 20
Enter the grade of student 5:
Test 1: 30
Test 2: 40
Test 3: 65
Enter the grade of student 6:
Test 1: 67
Test 2: 87
Test 3: 90
Enter the grade of student 7:
Test 1: 30
Test 2: 29
Test 3: 49
Enter the grade of student 8:
Test 1: 54
Test 2: 65
Test 3: 67
Enter the grade of student 9:
Test 1: 89
Test 2: 90
Test 3: 98
Enter the grade of student 10:
Test 1: 54
Test 2: 23
Test 3: 50
The average grades are given below:
Average grade of student 1 = 50
Average grade of student 2 = 60
Average grade of student 3 = 63.3333
Average grade of student 4 = 30
Average grade of student 5 = 45
Average grade of student 6 = 81.3333
Average grade of student 7 = 36
Average grade of student 8 = 62
Average grade of student 9 = 92.3333
Average grade of student 10 = 42.3333