In: Computer Science
Directions:
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.
So below is my code so far.
I'm getting the error "readData (inputFile, name, scores)"
Is there another way to do this? Please help thanks.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX = 50;
void getdata(ifstream& infile, string name[],int grades[][5], int& n)
{
n = 0;
int i = 0, j = 0;
while (!infile.eof())
{
infile >> name[i];
for (int j = 0; j < 5; j++)
infile >> grades[i][j];
i++;
}
n = i;
}
char calculateGrade(double average)
{
if(average >=90)
cout<<"A" << endl;
else if(average >=80)
cout<<"B" << endl;
else if(average >=70)
cout<<"C\n";
else if(average >=60)
cout<<"D\n";
else
cout<<"F\n";
return 0;
}
void calculateaverage(int a[][5],char grade[], double avg[])
{
for(int i = 0; i < 10; i++)
{
double sum = 0.0;
for (int j = 0; j<5; j++)
sum += a[i][j];
avg[i] = sum / 5.0;
grade[i] = calculateGrade(avg[i]);
}
}
void print(string names[], double avg[], int scores[][5], char grade[], int n)
{
cout << setw(10) << "Student name" << setw(20) << "Test Scores" << endl;
for (int i = 0; i<n; i++) {
cout << setw(10) << names[i];
for (int k = 0; k < 5; k++)
cout << setw(8) << scores[i][k];
cout << endl;
}
}
int main()
{
string name[MAX];
int scores[MAX][5];
double avg[MAX];
char grade[MAX];
ifstream inputFile("/Users/veronicadean/Desktop/1501/RandomTemp/2110/Lab8");
if(!inputFile)
{
cout << "Unable to open the input file.";
return 1;
}
readData (inputFile, name, scores)
inputFile.close();
calculateaverage (scores, grade, avg);
cout << name << avg << scores << grade;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX = 50;
void getdata(ifstream& infile, string name[],int grades[][5], int& n)
{
n = 0;
int i = 0, j = 0;
while (!infile.eof())
{
infile >> name[i];
for (int j = 0; j < 5; j++)
infile >> grades[i][j];
i++;
}
n = i;
}
char calculateGrade(double average)
{
if(average >=90)
return 'A';
else if(average >=80)
return 'B';
else if(average >=70)
return 'C';
else if(average >=60)
return 'D';
else
return 'F';
return 0;
}
void calculateaverage(int a[][5],char grade[], double avg[])
{
for(int i = 0; i < 10; i++)
{
double sum = 0.0;
for (int j = 0; j<5; j++)
sum += a[i][j];
avg[i] = sum / 5.0;
grade[i] = calculateGrade(avg[i]);
}
}
void print(string names[], double avg[], int scores[][5], char grade[], int n)
{
cout << setw(10) << "Student name" << setw(20) << "Test Scores" << setw(30) << "Average" << setw(10) << "Grade" << endl;
for (int i = 0; i<n; i++) {
cout << setw(10) << names[i];
for (int k = 0; k < 5; k++){
cout << setw(8) << scores[i][k];
}
cout << setw(10) << avg[i];
cout << setw(10) << grade[i];
cout << endl;
}
}
int main()
{
string name[MAX];
int scores[MAX][5];
int n;
double avg[MAX];
char grade[MAX];
ifstream inputFile("data.txt");
if(!inputFile)
{
cout << "Unable to open the input file.";
return 1;
}
getdata (inputFile, name, scores, n);
inputFile.close();
calculateaverage (scores, grade, avg);
print(name, avg, scores, grade, n);
return 0;
}
Also i am attaching screenshot of code and output for the same.
Note: My input file name was data.txt
Output: