This assignment involves writing code that processes information about the students in a class. You will need to use the following declarations:
#include <string> #include <fstream> const int NUMGRADES = 5; struct StudentInfo { string id; int grades[NUMGRADES]; double avGrade; };You are to write two C++ functions called ReadStudents and HighestAverage, whose headers are given below, as well as a main function that tests the other two.
int ReadStudents(istream &input, StudentInfo Info[]); void HighestAverage(StudentInfo Info[], int numStudents, string &id, double &maxAv);
ReadStudents should read (from the given input stream input) information about students and their grades, and should store information about each student in the Info array (one array entry for each student). ReadStudents should also return the number of student records that were read.
You should assume that the array is large enough to hold information about all of the students. (However, note that the number of student records in the input file is not one of ReadStudents's parameters; if you use code like the following:
string oneId; while (input >> oneId) { ... }the while loop will terminate when input is at end-of-file.)
Assume that for each student, the input contains the student's ID (a 9-digit number), followed by the student's grades (NUMGRADES of them), with the ID and the grades separated by whitespace (spaces, tabs, or newlines). ReadStudents should store each student's ID, grades, and average grade in one element of the Info array.
HighestAverage should determine which student in the Info array has the highest average grade, and should set its id and maxAv parameters to the value of that student's ID and average.
Finally, your main function should be written to expect one command-line argument: the name of the file that contains student data. It should open that file for reading, then call ReadStudents to fill in an array of information about the students, then call HighestAverage to determine which student has the highest average, and finally should print (to the standard output), the message:
Student x has the highest average: y(Where x is the student's ID, and y is the student's average grade.)
Here is how to have your main function open the file named on the command line for input:
#include <fstream> int main(int numargs, char *args[]) { ifstream inFile; // make sure the file name was given as a command-line arg if (numargs < 2) { cerr << "usage: " << args[0] << " <file name>" << endl; exit(1); } // open the file inFile.open(args[1]); // make sure the open worked if (inFile.fail()) { cerr << "Unable to open " << args[1] << endl; exit(1); } }Note that in C++ (unlike Java), args[0] always contains the name of the program being executed. The other values in the args array are the command-line arguments.
# include "prog1.h"To test your program, create an executable by typing:
g++ main.C prog1.C(this will create an executable named a.out).
What to turn-in: | prog1.C prog1.h main.C |