write the program in C++ only Write a program that inputs student grades, calcul
ID: 3806009 • Letter: W
Question
write the program in C++ only
Write a program that inputs student grades, calculates and prints the average and the corresponding letter grade. Create the following functions. REMEMBER TO CODE AND TEST ONE FUNCTION AT A TIME!
getScore Receives no input. Prompts the user to input a test score. Include a loop to validate that the score is between 0 and 100. Returns a validated score. Main will call this function 3 times to enter and store the 3 numbers.
calcAverage Receives 3 test scores as input, calculates and returns the average.
calcGrade Receives as input an average, determines and returns the letter grade. Use the standard grading scale: A = 90 to 100, B = 80 to less than 90 C = 70 to less than 80, D = 60 to less than 70, F = below 60
Note that the average and grade are printed in main (NOT in the function). Use a loop in main to continue to process more data.
Sample Output (Test case 1)
Enter test score: 70
Enter test score: 80
Enter test score: 110
Invalid, please re-enter: 92
Your average is 80.7
Your grade is B
Another? y
(continue processing other data listed below)
Additional test data:
Test scores of: 55, 88, 78
Test scores of: 85, 90, 98
Test scores of: 50, 62, 55
Test scores of 58, 65, 68
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int getScore() {
int score;
cout<<"Enter test score: ";
cin >> score;
while(score < 0 || score > 100) {
if(score < 0 || score > 100){
cout<<"Invalid, please re-enter: ";
cin >> score;
}
}
return score;
}
double calcAverage (int score1, int score2, int score3) {
return (score1 + score2 + score3)/(double)3;
}
char calcGrade (double average){
char grade;
if(average >= 90) {
grade ='A';
}
else if(average >=80 && average < 90){
grade ='B';
}
else if(average >=70 && average < 80){
grade ='C';
}
else if(average >=60 && average < 70){
grade ='D';
}
else{
grade = 'F';
}
return grade;
}
int main(){
int score1 = getScore();
int score2 = getScore();
int score3 = getScore();
double average = calcAverage( score1, score2, score3);
cout<<"Your average is "<<average <<endl;
cout<<"Your grade is "<<calcGrade(average)<<endl;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter test score: 70
Enter test score: 80
Enter test score: 110
Invalid, please re-enter: 92
Your average is 80.6667
Your grade is B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.