Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that will call a function named readGrades() to read a file

ID: 3560219 • Letter: W

Question

Write a C++ program that will call a function named readGrades() to read a file called gradex.txt that contains student names and three exam grades for each student. Next, the program will call a function named GradesCalc() that will calculate the average and displays the average. Next, it will call another function named letterGrade that will display the letter grade. Finally, the program will call a function named writeGrades() that will append the calculated letter grade for each student to grades.txt file. The letterGrade function accepts the calculated average grade as the input and returns no value. Letter grade assignment is given below. Grades are assumed to be between 0 and100; grades outside the range would not be counted toward the calculations. Grade Overall Average Range A 90-100 B 80-89 C 70-79 D 60-69 F <60

Explanation / Answer

# include <iostream>

# include <string>

# include <fstream>

using namespace std;

struct studentType

{

    string firstname;

    string lastname;

    double midterm;

    double final;

    double homework[3];

    double hmavg;

    double average;

    char grade;

};

int arraySize();

void copyFromFileToArray (studentType *s, int n);

void displayData (studentType *s, int n);

void homeworkAverage(studentType *s, int n);

void averageAll (studentType *s, int n);

void grade (studentType *s, int n);

void arrangeAlphabetically(studentType *s, int n);

void arrangeByGrades(studentType *s, int n);

int main()

{

    int n = arraySize();

    studentType *s = NULL;

    s = new studentType[n];

    copyFromFileToArray(s, n);

    homeworkAverage(s, n);

    averageAll(s, n);

    grade(s, n);

    cout << " - Press 1: to print the data in alphabetical order ";

    cout << " - Press 2: to print the data according to their grades ";

    cout << " Your Choice is: ";

    int choice; cin >> choice;

    switch (choice)

    {

    case 1:

        arrangeAlphabetically(s, n);

        break;

    case 2:

        arrangeByGrades(s, n);

        break;

    }

    displayData(s, n);

    return 0;

}

int arraySize()

{

    ifstream inFile;

    inFile.open("grades.txt");

    int size = 0;

    string fn, ln;

    double m, f;

    double h[5];

    while (!inFile.fail())

    {

        inFile >> fn;

        inFile >> ln;

        inFile >> m;

        inFile >> f;

        for (int j=0; j<5; j++)

            inFile >> h[j];

        size++;

    }

    inFile.close();

    return (size-1);

}

void copyFromFileToArray(studentType *s, int n)

{

    ifstream inFile;

    inFile.open("grades.txt");

    for(int i=0; i<n; i++)

    {

        inFile >> s[i].firstname;

        inFile >> s[i].lastname;

        inFile >> s[i].midterm;

        inFile >> s[i].final;

        for (int j=0; j<5; j++)

            inFile >> s[i].homework[j];

    }

    inFile.close();

}

void homeworkAverage(studentType *s, int n)

{

    double max, hmSum;

    int indexMax;

    for(int k=0; k<n; k++)

    {

        for (int i=0; i<4; i++)

        {

            max = s[k].homework[i];

            indexMax = i;

            for (int j=i+1; j<5; j++)

            {

                if (s[k].homework[j]>max)

                {

                    max = s[k].homework[j];

                    indexMax = j;

                    double temp = s[k].homework[i];

                    s[k].homework[i] = s[k].homework[indexMax];

                    s[k].homework[indexMax] = temp;

                }

            }

        }

    }

    for (int i=0; i<n; i++)

    {

        hmSum = s[i].homework[0]+s[i].homework[1]+s[i].homework[2];

        s[i].hmavg = ((hmSum/3)/10)* 100;

    }

}

void averageAll (studentType *s, int n)

{

    for (int i=0; i<n; i++)

    {

        s[i].average = ((s[i].midterm/100*30) + (s[i].hmavg/100*30) + (s[i].final/100*40));

    }

}

void displayData (studentType *s, int n)

{

    cout << " Name Midterm HMAvg Final Avg Grade ";

    cout << "___________________________________________________________ ";

    for (int i=0; i<n; i++)

    {

        cout << s[i].firstname << " ";

        cout << s[i].lastname << " ";

        cout << s[i].midterm << " ";

        cout << s[i].hmavg << " ";

        cout << s[i].final << " ";

        cout << s[i].average << " ";

        cout << s[i].grade;

        cout << endl << endl;

    }

    cout << endl;

}

void grade(studentType *s, int n)

{

    char grade;

    for (int i=0; i<n; i++)

    {

        if(s[i].average<=100 && s[i].average>=90)

            grade = 'A';

        else if(s[i].average<90 && s[i].average>=80)

            grade = 'B';

        else if(s[i].average<80 && s[i].average>=70)

            grade = 'C';

        else if(s[i].average<70)

            grade = 'F';

        s[i].grade = grade;

    }

}

void arrangeAlphabetically(studentType *s, int n)

{

    string first;

    int indexFirst;

    for(int i=0; i<n-1; i++)

    {

        first = s[i].firstname;

        for(int j=i+1; j<n; j++)

        {

            if(s[j].firstname[0]<first[0])

            {

                first = s[j].firstname;

                indexFirst = j;

                studentType temp = s[i];

                s[i] = s[indexFirst];

                s[indexFirst] = temp;

            }

        }

    }

}

void arrangeByGrades(studentType *s, int n)

{

    studentType min;

    int indexMin;

    for(int i=0; i<n-1; i++)

    {

        min.grade = s[i].grade;

        for(int j=i+1; j<n; j++)

        {

            if(s[j].grade<min.grade)

            {

                min.grade = s[j].grade;

                indexMin = j;

                studentType temp = s[i];

                s[i] = s[indexMin];

                s[indexMin] = temp;

            }

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote