Write a complete C++ program to calculate the average grade for a student. Displ
ID: 3590866 • Letter: W
Question
Write a complete C++ program to calculate the average grade for a student. Display a prompt on the monitor ask a user: “ How many grades do you want to process? “. After user has entered how many grades he/she wants to process, create a loop that will allow the user to enter his/her grades ( grades should be in the range of 0 to 100, Your program should validate that grades entered are in the range of 0 to 100 and reject grades that are not in this range).
After user has entered all of his/her grades your program should calculate the average grades and display the average grades.
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int n,grade;
float sum=0;
//get user input of number of grades
cout << "How many grades do you want to process?.";
cin >> n;
for(int i=0;i<n;i++){
cout << "Enter grade"<< i+1 << ":";
cin >> grade;
//validate grades
while(grade<0 || grade>100){
cout << "Invalid grade" << endl;
cout << "Enter grade"<< i+1 << ":";
cin >> grade;
}
//add grade to sum
sum += grade;
}
//calculate average and print
cout << "Avg: " << sum/n << endl;
return 0;
}
/*
sample output
How many grades do you want to process?. 3
Enter grade1: 0
Enter grade2: 101
Invalid grade
Enter grade2: 100
Enter grade3: 55
Avg: 51.6667
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.