Write a program to calculate students’ average test scores and their grades. You
ID: 3628646 • Letter: W
Question
Write a program to calculate students’ average test scores and their grades. You may assume the following input data:
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
Use three arrays: a one-dimensional array to store the students’ names, a (parallel) two-dimensional array to store the test scores, and a parallel one-dimensional array to store grades. Your program must contain at least the following functions: a function to read and store data into two arrays, a function to calculate the average test score and grade, and a function to output the results. Have your program also output the class average.
Explanation / Answer
#include <iostream>
#define STUDENTS 5
#define GRADES 4
using namespace std;
void getname(char[]);
void getgrades(char[],double[]);
char calcgrade(double[],double&);
void print(char[],double[],char,double);
int main()
{char name[STUDENTS][20],letter[STUDENTS],grade;
double s1[GRADES],s2[GRADES],s3[GRADES],s4[GRADES],s0[GRADES],avg;
getname(name[0]);
getgrades(name[0],s0);
grade=calcgrade(s0,avg);
print(name[0],s0,grade,avg);
cin.ignore();
getname(name[1]);
getgrades(name[1],s1);
grade=calcgrade(s1,avg);
print(name[1],s1,grade,avg);
cin.ignore();
getname(name[2]);
getgrades(name[2],s2);
grade=calcgrade(s2,avg);
print(name[2],s2,grade,avg);
cin.ignore();
getname(name[3]);
getgrades(name[3],s3);
grade=calcgrade(s3,avg);
print(name[3],s3,grade,avg);
cin.ignore();
getname(name[4]);
getgrades(name[4],s4);
grade=calcgrade(s4,avg);
print(name[4],s4,grade,avg);
system("pause");
return 0;
}
void getname(char n[])
{cout<<"Enter students name: ";
cin.getline(n,20);
}
void getgrades(char n[],double g[])
{int i;
cout<<"Enter grades for "<<n<<endl;
for(i=0;i<GRADES;i++)
{cout<<"Enter grade "<<i+1<<": ";
cin>>g[i];
while(g[i]<0||g[i]>100)
{cout<<"Invalid grade ";
cout<<"Enter grade "<<i+1<<": ";
cin>>g[i];
}
}
}
char calcgrade(double g[],double& a)
{int i;
double sum=0;
char grade;
double min=g[0];
for(i=1;i<GRADES;i++)
if(g[i]<min)
min=g[i];
for(i=0;i<GRADES;i++)
sum+=g[i];
sum-=min;
a=sum/(GRADES-1);
switch((int)a/10) // using integer arithmetic by dividing
{case 10: // score by 10 anything between 90 to 99 becomes 9
case 9: // anything between 80 to 89 becomes 8 etc.
grade='A';
break;
case 8:
grade='B';
break;
case 7:
grade='C';
break;
case 6:
grade='D';
break;
default:
grade='F';
}
return grade;
}
void print(char n[],double g[],char l,double a)
{int i;
cout<<"name: "<<n<<endl;
cout<<"grades: ";
for(i=0;i<GRADES;i++)
cout<<g[i]<<" ";
cout<<" average: "<<a<<endl;
cout<<"grade: "<<l<<endl<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.