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.
#include <iostream>
#include<string>
using namespace std;
double getAvg(int number, int score[][5])
{
double sum_score = 0;
for (int i = 0; i < 5; i++)
{
sum_score += score[number][i];
}
return sum_score / 5;
}
void setGlade(int number ,int score [][5],string *glade)
{
double avg_score;
avg_score = getAvg(number, score);
if (avg_score >= 90 && avg_score <= 100)
{
glade[number] = "A";
}
else if (avg_score >= 80 && avg_score <= 89)
{
glade[number] = "B";
}
else if (avg_score >= 70 && avg_score <= 79)
{
glade[number] = "C";
}
else if (avg_score >= 60 && avg_score <= 69)
{
glade[number] = "D";
}
else if (avg_score < 60)
{
glade[number] = "F";
}
}
string getGlade(int number, int score[][5], string* glade)
{
setGlade( number, score, glade);
return glade[number];
}
void setScore(int number, int score[][5])
{
for (int i = 0; i < 5; i++)
{
cin >> score[number][i];
}
}
void setName(int number, string* name)
{
cin>> name[number];
}
string getName(int number, string* name)
{
return name[number];
}
void Display(int number, int score[][5], string* glade,string *name)
{
cout << "Name:" << getName(number, name)<<endl;
cout << "Test score: ";
for (int i = 0; i < 5; i++)
{
cout << score[number][i] << " ";
}
cout << endl;
cout << "Glade " << getGlade(number, score, glade) << endl;
cout << "Avg score " << getAvg(number, score);
}
int main()
{
string names[10];
int test_score[10][5];
string test_glade[10];
// enter all student example
for (int i = 0; i < 10; i++)
{
cout << "Enter name " << i + 1 << " student: ";
setName(i, names);
cout << "Enter test score: ";
setScore(i, test_score);
}
// view all student info
for (int i = 0; i < 10; i++)
{
Display(i, test_score, test_glade, names);
}
// output the class average
float all_avg=0;
for (int i = 0; i < 10; i++)
{
all_avg += getAvg(i, test_score);
}
all_avg /= 10;
cout <<"The class average." << all_avg << endl;
return 0;
}
Comments
Leave a comment