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

Write a complete C++ program that will open a file called \"grade.txt\" and will

ID: 3642294 • Letter: W

Question

Write a complete C++ program that will open a file called "grade.txt" and will read in all of the names and grades stored in the file into arrays. Compute and print the number of grades and the average grade. Then print the naqmes of all of the students that made a grade higher than the average.
Here is a sample data file and the sample output:
grade.txt output of the program
There were 7 grades
Bob 91.5 The average grade was 72.3571
Bill 88 The following students were above average:
Tim 75 Bob
Kevin 99 Bill
Sue 50 Tim
Greg 10 Kevin
Mark 93 Mark

Explanation / Answer

This solution allows for a maximum of 10 grades in the grade.txt file. If the limit should be higher/lower, the arrays[10] and if (num_read < 9) should be adjusted accordingly.

using namespace std;

#include <iostream>
#include <fstream>
#include <string>

int main() {
    string names[10];    // allocate array to hold up to 10 names
    float grades[10];    // allocate array to hold up to 10 grades
    ifstream inFile;     // ifstream for input file
    int num_read = 0;    // count # of items read from file
    float average = 0.0; // average grade
   
    inFile.open("grade.txt");          // open the file
    if (!inFile) {                     // check for success
        cout << "Unable to open file"; // notify if error
        exit(1);                       // terminate with error
    }

    while (inFile >> names[num_read] >> grades[num_read]) {
    // read name & grade from file
        if (num_read < 9) {
        // make sure we don't overflow the arrays
            average += grades[num_read]; // add this grade to average
            num_read++; // increment number read
        }
        else {
            break; // break out of while loop if more than 10 records
        }
    }
  
    inFile.close(); // close the file

    average /= num_read; // divide accumulated grades by num_read to get average
   
    cout << "The average grade was " << average << endl; // display the average
    cout << "The following stuents were above average:" << endl;

    for (int i = 0; i <= num_read; i++) { // check for students above the average
        if (grades[i] > average) { // if grade is > average
            cout << " " << names[i] << endl; // display the name of student
        }
    }

    system("Pause"); // pause to review output

    return 0; // done
}

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