Program must be written in C Practicing an example of functi

Program must be written in C++:

Practicing an example of function using call by reference) Write a program that reads a set of information related to students in C++ class and prints them in a table format. The information is stored in a file called data.txt. Each row of the file contains student number, grade for assignment 1, grade for assignment 2, and grade for assignment 3. Your main program should read each row, pass the grades for the three assignments to a function called ProcessARow to calculate the average of the grades, minimum of the three assignments, and the maximum of the three assignments. The results (average, maximum, and minimum) should be returned to the main program and the main program prints them on the screen in a table format. For example, if the file includes 126534 9 8 10 321345 7 3 5 324341 9 9 9 your program should print Std-Id A1 A2 A3 Min Max Average ------------------------------------------------------------------------- 126534 9 8 10 8 10 9.0 321345 7 3 5 3 7 5.0 324341 9 9 9 9 9 9.0 You must use call by reference to do the above question.

Solution

Here is the c++ code:

#include <iostream>

#include <iomanip>

#include <iterator>

#include <vector>

#include <fstream>

using namespace std;

/*

126534 9 8 10

321345 7 3 5

324341 9 9 9

*/

class CGrades

{

private:

int iStudentID;

int iGrades[3];

int iMin, iMax;

double dblAvg;

public:

void calculate()

{

dblAvg = 0.0;

iMin = iGrades[0];

iMax = iGrades[0];

dblAvg = iGrades[0];

for (int i = 1; i < 3; i++)

{

iMin = iGrades[i] < iMin ? iGrades[i] : iMin;

iMax = iGrades[i] > iMax ? iGrades[i] : iMax;

dblAvg += iGrades[i];

}

dblAvg /= 3.0;

}

CGrades(const int &id = 0, const int &ia1 = 0, const int &ia2 = 0, const int &ia3 = 0) : iStudentID(id)

{

iGrades[0] = ia1;

iGrades[1] = ia2;

iGrades[2] = ia3;

}

friend istream& operator>>(istream& is, CGrades& g)

{

is >> g.iStudentID >> g.iGrades[0] >> g.iGrades[1] >> g.iGrades[2];

g.calculate();

return is;

}

friend ostream& operator<<(ostream& os, const CGrades& g)

{

cout << left << setw(6) << g.iStudentID << right << setw(3) << g.iGrades[0] << right << setw(3) << g.iGrades[1] << right << setw(3) << g.iGrades[2] << setw(3) << g.iMin << setw(4) << g.iMax << setw(6) << fixed << setprecision(1) << right << g.dblAvg;

return os;

}

};

int main()

{

vector<CGrades> grades;

ifstream fs(\"data.txt\");

copy(istream_iterator< CGrades >(fs), istream_iterator< CGrades >(), back_inserter( grades ));

cout << \"Std-Id A1 A2 A3 Min Max Average\ \";

cout << \"-------------------------------\ \";

copy(grades.begin(), grades.end(), ostream_iterator< CGrades >(cout, \"\ \"));

return 0;

}

Program must be written in C++: Practicing an example of function using call by reference) Write a program that reads a set of information related to students i
Program must be written in C++: Practicing an example of function using call by reference) Write a program that reads a set of information related to students i
Program must be written in C++: Practicing an example of function using call by reference) Write a program that reads a set of information related to students i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site