Cpp/C++ read grades from file and show average grades per student


Download code here main.cpp
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int readFile(const string &fileName, int studentId[], double grades[], int maxGrades);
double minimumGrade(const double grades[], int numberGrades);
void displayForStudent(int id, const int studentId[], const double grades[], int numberGrades);
void displayReverse(const int studentId[], const double grades[], int numberGrades);
double maximumGrade(const double grades[], int numberGrades);
double averageGrade(const double grades[], int numberGrades);
int main(){
    const int max = 20;
    int studentId[max];
    double grades[max];
    string fileName;
    int count;
    int i;
    do{
        cout << "Enter the file name to read or hit enter to exit" << endl;
        if(!getline(cin,fileName) || 0 == fileName.size()){
            break;
        }
        count = readFile(fileName, studentId, grades, max);
        cout << "There are ";
        if(count){
            cout << count;
        } else {
            cout << "no";
        }
        cout << " grade records" << endl;
        if(count){
            displayReverse(studentId, grades, count);
            cout << "Minimum grade is " << minimumGrade(grades,count) << endl;
            cout << "Maximum grade is " << maximumGrade(grades,count) << endl;
            cout << "Average grade is " << averageGrade(grades,count) << endl;
            for(i = 1; 6 >= i; i++){
                displayForStudent(i, studentId, grades, count);
            }
        }
    }while(1);
    return 0;
}

int readFile(const string &fileName, int studentId[], double grades[], int maxGrades){
    ifstream in(fileName.c_str());
    int index = 0;
    if(in){
        while(maxGrades > index && in >> studentId[index] >> grades[index]){
            index ++;
        }
        in.close();
    } else {
        cout << "File \"" << fileName << "\" could not be opened" << endl;
    }
    return index;
}

double minimumGrade(const double grades[], int numberGrades){
    int i = numberGrades - 1;
    double low = grades[i];
    while(i--){
        if(low > grades[i]){
            low = grades[i];
        }
    }
    return low;
}

void displayForStudent(int id, const int studentId[], const double grades[], int numberGrades){
    bool found = false;
    float count = 0.0f;
    double total = 0.0;
    cout << endl << "Grade for student " << id << endl;
    for(int i = 0; numberGrades > i; i++){
        if(studentId[i] == id){
            found = true;
            cout << grades[i] << endl;
            total += grades[i];
            count ++;
        }
    }
    if(!found){
        cout << "There are no grades for this student" << endl;
    } else {
        cout << "Average for student " << id << " is " << fixed << setprecision(2) << (total/count) << endl;
    }
}

void displayReverse(const int studentId[], const double grades[], int numberGrades){
    int i = numberGrades;
    cout << "Student id" << setw(7) << ' '  << "Grade" << endl;
    while(i--){
        cout << setw(10) << right << studentId[i];
        cout << setw(5) << ' ';
        cout << setw(7) << right << fixed << setprecision(2) << grades[i] << endl;
    }
}

double maximumGrade(const double grades[], int numberGrades){
    int i = numberGrades - 1;
    double high = grades[i];
    while(i--){
        if(high < grades[i]){
            high = grades[i];
        }
    }
    return high;
}

double averageGrade(const double grades[], int numberGrades){
    double total = 0.0;
    int i = numberGrades - 1;
    while(i--){
        total += grades[i];
    }
    return (total/(1.0 * numberGrades));
}

Comments

Popular posts from this blog