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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <ostream>
using namespace std;
const int NUMBER_OF_PEOPLE = 10;
const int NUMBER_OF_TESTS = 5;
void read(string name[], int namesSize, double scores[][NUMBER_OF_TESTS], int scoresRowSize)
{
ifstream infile;
infile.open("input.txt");
int counterNames =0, counterScores = 0;
while(counterNames < namesSize)
{
infile >> name[counterNames];
while( counterScores < NUMBER_OF_TESTS)
{
infile >> scores[counterNames][counterScores];
counterScores++;
}
counterScores = 0;
counterNames++;
}
}
void calculate(double matrix[], double scores[][NUMBER_OF_TESTS], int scoresRow_Size)
{
int counter = 0, num = 0;
double total = 0, average = 0;
cout << fixed << showpoint << setprecision(2) << endl;
while (counter < scoresRow_Size)
{
while (num < NUMBER_OF_TESTS)
{
total = total + scores[counter][num];
num++;
}
average = (total/NUMBER_OF_TESTS);
matrix[counter] = average;
total = 0;
average = 0;
num = 0;
counter++;
}
}
void print(double matrix[], string names[], int namesSize)
{
cout << setw(10) << "Name" << setw(12) << "Grade" << endl;
int counter = 0;
while (counter < namesSize)
{
cout << setw(10) << names[counter] << setw(12) << matrix[counter] << endl;
counter++;
}
}
int main()
{
string names[NUMBER_OF_PEOPLE];
double scores[NUMBER_OF_PEOPLE][NUMBER_OF_TESTS];
double grades[NUMBER_OF_PEOPLE];
read(names, 10, scores, 10);
calculate(grades, scores, 10);
print(grades, names, 10);
return 0;
}
|