lundi 28 octobre 2019

How could I make this code more efficient?

I'm what you would describe a newbie when it comes to C++ programming. I'm wondering if there is anyway for me to output the average grade reusing the code I did for ascertaining the grade in the first place? Any help would be greatly appreciated. A point in the right direction is all I'm looking for!

  #include <iostream>
    using namespace std;
    //THIS allows the user to input the students' name and grade.
    void get_student_info(string student_name[], int student_grade[], int array_size)
    {
        for (int iCount = 0; iCount < array_size; iCount++)
        {
            cout << "Enter the name for student " << iCount + 1 << ": ";
            cin >> student_name[iCount];
        }
        for (int iCount = 0; iCount < array_size; iCount++)
        {
            cout << "What grade did " << student_name[iCount] << " get? ";
            cin >> student_grade[iCount];
        }
        cout << endl;
    }
    //THIS function finds the overall average score.
    int average_score(int student_grade[], int array_size){
        int total_sum = 0;
        int average = 0;
        for (int iCount = 0; iCount < array_size;iCount++){
            total_sum += student_grade[iCount];
        }
        average = total_sum / array_size;
        return average;
    }
    //THIS ascertains the grade each of the students' get.
    void ascertain_grade(int student_grade[], int array_size, string student_name[])
    {
        for (int iCount = 0; iCount < array_size; iCount++){
            if (student_grade[iCount] >= 0 && student_grade[iCount] <= 10){
                cout << student_name[iCount] << "got a grade of F" << endl;
            }
            else if(student_grade[iCount] >= 11 && student_grade[iCount] <= 30){
                cout << student_name[iCount] << " scored " << student_grade[iCount] << " giving him a grade of E.\n";
            }
            else if(student_grade[iCount] >= 31 && student_grade[iCount] <= 50){
                cout << student_name[iCount] << " scored " << student_grade[iCount] << " giving him a grade of D.\n";
            }
            else if(student_grade[iCount] >= 51 && student_grade[iCount] <= 70){
                cout << student_name[iCount] << " scored " << student_grade[iCount] << " giving him a grade of C.\n";
            }
            else if(student_grade[iCount] > 71 && student_grade[iCount] <= 80){
                cout << student_name[iCount] << " scored " << student_grade[iCount] << " giving him a grade of B.\n";
            }
            else if(student_grade[iCount] > 81 && student_grade[iCount] <= 90){
                cout << student_name[iCount] << " scored " << student_grade[iCount] << " giving him a grade of A.\n";
            }
            else if(student_grade[iCount] > 91 && student_grade[iCount] <= 100){
                cout << student_name[iCount] << " scored " << student_grade[iCount] << " giving him a grade of A*.\n";
            }
            else{
                cout << "Invalid score. Must be out of 100\n";
            }
        }
    }
    //THIS is the piece of code I'm having problems with, it doesn't display the highest scorer, it just shows blank.
    void highest_score_scorer(int student_grade[], string student_name[], int array_size){
        int highest_score = student_grade[0];
        string highest_scorer = student_name[0];
        for (int iCount = 0; iCount < array_size; iCount++){
            if (student_grade[iCount] > highest_score){
                highest_score = student_grade[iCount];
                highest_scorer = student_name[iCount];
            }
        }
          cout << "The highest scorer is " << highest_scorer << " who scored " << highest_score << ".\n";
    }

    string average_grade(int student_grade[], int array_size){

        int average = average_score(student_grade, array_size);

        if (average >= 0 && average <= 10){
            return "F";
        }
        else if(average >= 11 && average <= 30){
            return "E.";
        }
        else if(average >= 31 && average <= 50){
            return " D.";
        }
        else if(average >= 51 && average <= 70){
            return "C.";
        }
        else if(average >= 71 && average <= 80){
            return "B.";
        }
        else if(average >= 81 && average <= 90){
            return "A.";
        }
        else if(average >= 91 && average <= 100){
            return "A*";
        }
        else{
            return "No average score could be calculated";
        }
    }

    int main() {

        int array_size = 5;
        string student_name[5] = {};
        int student_grade[5] = {};
        get_student_info(student_name, student_grade, array_size);
        ascertain_grade(student_grade, array_size, student_name);
        highest_score_scorer(student_grade, student_name, array_size);
        cout << "Average mark: " << average_score(student_grade, array_size) << endl;
        cout << "Average grade achieved: " << average_grade(student_grade, array_size) << endl;


        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire