#include using namespace std; double average(int sum_of_grades,int num_grades) { return sum_of_grades/(float)num_grades; } int main() { int num_grades,grade,sum=0; char grade_value; cout<<"Enter the number of grades"<>num_grades; for(int i=0;i>grade; sum+=grade; } double avg=average(sum,num_grades); if(avg>=90 && avg<=100) grade_value='A'; else if(avg>=80 && avg<=89) grade_value='B'; else if(avg>=70 && avg<=79) grade_value='C'; else if(avg>=60 && avg<=69) grade_value='D'; else if(avg>=0 && avg<=59) grade_value='F'; cout<<"The grade is "<
#include <iostream>
using namespace std;
double average(int sum_of_grades,int num_grades)
{
return sum_of_grades/(float)num_grades;
}
int main() {
int num_grades,grade,sum=0;
char grade_value;
cout<<"Enter the number of grades"<<endl;
cin>>num_grades;
for(int i=0;i<num_grades;i++)
{
cout<<"Enter a numeric grade between 0-100"<<endl;
cin>>grade;
sum+=grade;
}
double avg=average(sum,num_grades);
if(avg>=90 && avg<=100)
grade_value='A';
else if(avg>=80 && avg<=89)
grade_value='B';
else if(avg>=70 && avg<=79)
grade_value='C';
else if(avg>=60 && avg<=69)
grade_value='D';
else if(avg>=0 && avg<=59)
grade_value='F';
cout<<"The grade is "<<grade_value;
}
review if the written c++ code is correct then organize the code and write comments for each part of the program explaining what they do.
Algorithm: CalculateLetterGrade
1. Include the necessary header file for input/output (<iostream>).
2. Declare a function named 'average' that takes the sum of grades and the number of grades as parameters and returns the average.
3. In the 'main' function:
a. Declare integer variables for the number of grades (num_grades), individual grades (grade), and the sum of grades (sum).
b. Declare a character variable to store the letter grade (grade_value).
4. Prompt the user to input the number of grades and store it in 'num_grades'.
5. Use a 'for' loop to input the grades from the user:
a. Inside the loop, display the message "Enter a numeric grade between 0-100".
b. Read and store the entered grade in 'grade'.
c. Add 'grade' to 'sum'.
6. Calculate the average of the grades by calling the 'average' function and store it in 'avg'.
7. Determine the letter grade based on the value of 'avg':
a. If 'avg' is between 90 and 100, set 'grade_value' to 'A'.
b. Else if 'avg' is between 80 and 89, set 'grade_value' to 'B'.
c. Else if 'avg' is between 70 and 79, set 'grade_value' to 'C'.
d. Else if 'avg' is between 60 and 69, set 'grade_value' to 'D'.
e. Else if 'avg' is between 0 and 59, set 'grade_value' to 'F'.
8. Output the calculated grade to the console with the message "The grade is <grade_value>".
9. End the program.
Step by step
Solved in 3 steps with 1 images