In: Computer Science
Declare local variables under main() program
Prompts the user to enter 5 test scores
Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg
Define a function to determine the letter grade: this should accept a test score as argument and return a letter grade based on the following grading scale.
Score Letter Grades
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F
I'm writing the code in C++:
Program:
#include <iostream>
using namespace std;
float calc_avg(int x1, int x2, int x3, int x4, int x5)//function
to calculate average
{
float avg;
avg=(x1+x2+x3+x4+x5)/5;//calculating average
return avg;
}
char assign_grade(int y1)//function to assign grade
{
char grade;
if(y1>=90 && y1<=100)//assigning grades according to
conditions
grade='A';
else if(y1>=80 && y1<=89)
grade='B';
else if(y1>=70 && y1<=79)
grade='C';
else if(y1>=60 && y1<=69)
grade='D';
else
grade='F';
return grade;
}
int main(){
int n1,n2,n3,n4,n5,a;
char g;
cout<<"Enter 5 test scores:\n";
cin>>n1>>n2>>n3>>n4>>n5;//taking 5
user test scores input
cout<<"\ngrades assigned:\n";
g=assign_grade(n1);//calling function for assigning grade
cout<<"n1\t"<<g<<"\n";
g=assign_grade(n2);
cout<<"n2\t"<<g<<"\n";
g=assign_grade(n3);
cout<<"n3\t"<<g<<"\n";
g=assign_grade(n4);
cout<<"n4\t"<<g<<"\n";
g=assign_grade(n5);
cout<<"n5\t"<<g<<"\n";
cout<<"\naverage value:\n";
a=calc_avg(n1,n2,n3,n4,n5);//calling function to calculate
average
cout<<a;
return 0;
}
Output: