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 <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
const int numrows=10;
const int numcol=5;
int maxIndex(double arr[], int size);
double calculateAverage(double arr[], int size);
void getData(char fileName[], string names[], double arr[][5], double avg[] , int *size);
void print(string Names[], double scores[][5], double avg[], int size);
int main()
{
int count = 0;
string Names[numrows];
double scores[numrows][numcol];
double averageScores[numrows];
//compute score and grades
getData("grades.txt", Names, scores, averageScores, &count);
//Outputs final results
print(Names, scores, averageScores, count);
//calculateAverages(averageScores, classAverage, numrows);
return 0;
}
int maxIndex(double arr[], int size){
int max = 0;
for(int i = 1; i < size; i++){
if(arr[i] > arr[max]) max = i;
}
return max;
}
double calculateAverage(double arr[], int size){
double avg = 0;
for(int i = 0; i < size; i++){
avg += arr[i];
}
avg /= size;
return avg;
}
void getData(char fileName[], string names[], double arr[][5], double avg[], int *size){
*size = 0;
ifstream inFile(fileName);
while(inFile >> names[*size]){
double x = 0;
for(int j = 0; j < 5; j++){
inFile >> arr[*size][j];
x += arr[*size][j];
}
avg[*size] = x / 5.0;
(*size)++;
}
}
void print(string Names[], double scores[][5], double avg[], int size){
cout << fixed << setprecision(2);
cout << "Name\tTest 1\tTest 2\tTest 3\tTest 4\tTest 5\tAverage\n";
for(int i = 0; i < size; i++){
cout << Names[i] << "\t";
for(int j = 0; j < 5; j++){
cout << scores[i][j] << "\t";
}
cout << calculateAverage(scores[i], 5) << "\n";
}
cout << "Class average: " << calculateAverage(avg, size) << "\n";
cout << "Student with highest grade: " << Names[maxIndex(avg, size)] << "\n";
}
|