1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
const int MAX = 50;
// a function called GetData to read and store data into two arrays,
void GetData(ifstream& infile,string name[],int scores[][5],int& n)
{
n = 0;
int i=0;
int j=0;
while(!infile.eof())
{
infile >> name[i];
for(int j=0; j<5; j++)
infile >> scores[i][j];
i++;
}
n = i;
}
// a function called Average that is used to calculate the average test score and grade,
void Average(int a[][5],double avg[],int no_of_students)
{
for(int i=0; i<no_of_students; i++)
{
double sum =0;
for(int j=0; j<5; j++)
sum+= a[i][j];
avg[i] = sum/static_cast<double> (5);
}
}
// function called PrintResults to output the results.
void PrintResults(string name[],double avg[],int scores[][5],int n)
{
for(int i=0; i<n; i++){
cout << left << setw(10)<< name[i];
for(int k=0; k<5; k++)
cout << right << setw(8) << scores[i][k];
cout << endl;
}
cout << setw(8) <<"Average ";
for(int i=0; i<n; i++)
cout << setw(5) << avg[i];
cout << endl;
}
int main()
{
// a one-dimensional array to store the students� names,
string name[MAX];
// a (parallel) two-dimensional array to store the test scores,
int scores[MAX][5];
// a parallel one-dimensional array to store grades.
int no_of_students;
double avg[MAX];
ifstream infile("_mp.txt");
if(!infile)
{
cout <<"unable to open file.so exiting from program" << endl;
return 0;
}
GetData(infile, name, scores, no_of_students);
infile.close();
Average(scores, avg, no_of_students);
PrintResults(name,avg,scores,no_of_students);
return 0;
}
|