question archive Make a program to compute numeric grades for a course
Subject:Computer SciencePrice:5.86 Bought14
Make a program to compute numeric grades for a course. First make a file and input the course records that will serve as the input. The input file is in exactly the following format:
Each line contains a student's last name, then one space, then the student's first name, then one space, then ten quiz scores all on one line. The quiz scores are whole numbers and are separated by one space. your program will take its input from this file and send its output to a second file.
The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student's ten quiz scores. Use at least one function that has file streams as all or some of its arguments.
Extend the program to also intake more student scores and correctly format them in the studentScores.txt file and give the user the option of running the program to create the file with the averages and then display the results after saving to the output file.
Data:
Mike Bucky 99 95 86 74 100 79 81 93 75 88
Tom Nancy 100 88 74 99 92 83 71 86 94 83
Program Approach -
Step-by-step explanation
Code ( C++ ) -
//header files
#include <fstream>
#include <iostream>
using namespace std;
//defining function named average_data to process the data
//and compute the average of scores
void average_data(ifstream &infile, ofstream &outfile) {
//variables declaration and initialization
string first_name, last_name;
int quiz_score;
double avrg = 0.0;
//while loop begins
while (!infile.eof()) {
//for reading the names of student
infile >> last_name >> first_name;
////for displaying the names of student in output file
outfile << last_name << " " << first_name << " ";
avrg = 0.0;
//for reading scores of student
for (int k = 0; k < 10; k++) {
infile >> quiz_score;
outfile << quiz_score << " ";
avrg += quiz_score;
}
///computing the average
avrg /= 10;
//displaying the average of each students scores
outfile << avrg << "\n";
}
}
//main function definition
int main() {
//opening the input file
ifstream infile("studentScores.txt");
//checking whether the file is opened or not
if (!infile) {
//if not then display the error message
cout << "Error: No such file found\n";
return 1;
}
//opening the output file
ofstream outfile("output.txt"); // Open output file
//calling the function named average_data()
average_data(infile, outfile);
//closing the input file
infile.close();
//closing the output file
outfile.close();
return 0;
}
Sample input
Sample output
Please see the attached file for the complete solution