Need help in c++, I have the code i need the output to display the avergae but i
ID: 3742058 • Letter: N
Question
Need help in c++, I have the code i need the output to display the avergae but it is not displaying please help and thank you
Program 1: (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, grade for assignment 3, and grade for assignment 4. Your main program should read each row, pass the grades for the three assignments to a function called ProcessRow to calculate the average of the grades, minimum of the four assignments, and the maximum of the four 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 7 8 10 7
321345 5 6 4 9
324341 8 3 8 5
your program should print
Std-Id A1 A2 A3 A4 Min Max Average
----------------------------------------------------------------------------------
126534 7 8 10 7 7 10 8
321345 5 6 4 9 4 9 6
324341 8 3 8 5 3 8 6
You must use call by reference to do the above question.
My code below
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//function prototype
void ProcessARow(int a1, int a2, int a3, int a4, int &min, int &max, float &average);
int main()
{
int id;
int a1;
int a2;
int a3;
int a4;
int min;
int max;
float average;
string filename = "data.txt";
ifstream infile;
infile.open (filename);
//if file is not found it will print out not found
if(!infile)
{
cout << filename << " is not found! " << endl;
system("pause");
exit(1);
}
//printing output heading
cout << left << setw(12) << "std-id" << setw(6) << "A1" << setw(6) << "A2" << setw(6) << "A3" << setw(6) << "A4" << setw(6) << "Min" << setw(6) << "Max" << setw(6) << "Average" << endl;
cout << "-------------------------------------------------------" << endl;
//reading student id
infile >> id;
while(infile)
{
//reading four grades
infile >> a1 >> a2 >> a3 >> a4;
//calling the processARow function
ProcessARow(a1, a2, a3, a4, min, max, average);
//displaying the details
printf("%-12d%-6d%-6d%-6d%-6d%-6d%-6.1f ", id, a1, a2, a3, a4, min, max, average);
//reading student id
infile >> id;
}
system("pause");
return 0;
}
//processARow function
void ProcessARow(int a1, int a2, int a3, int a4, int &min, int &max, float &average)
{
//finding min vaule
if(a1 < a2 && a1 < a3 && a1 < a4)
min = a1;
else if(a2 < a3)
min = a2;
else if(a3 < a4)
min = a3;
else
min = a4;
//finding max
if(a1 > a2 && a1 > a3 && a1 > a4)
max = a1;
else if(a2 > a3)
max = a2;
else if(a3 > a4)
max = a3;
else
max = a4;
//getting average
average = (a1 + a2 + a3 + a4) / 4.0;
}
Explanation / Answer
UPDATED PROGRAM USING C++
/* data.txt File
126534 7 8 10 7
321345 5 6 4 9
324341 8 3 8 5 */
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//function prototype
void ProcessARow(int a1, int a2, int a3, int a4, int &min, int &max, float &average);
int main()
{
int id;
int a1;
int a2;
int a3;
int a4;
int min;
int max;
float average;
char filename[20] = "f:\dataStd.txt";
ifstream infile;
infile.open (filename);
//if file is not found it will print out not found
if(!infile)
{
cout << filename << " is not found! " << endl;
system("pause");
exit(1);
}
//printing output heading
cout << left << setw(12) << "std-id" << setw(6) << "A1" << setw(6) << "A2" << setw(6) << "A3" << setw(6) << "A4" << setw(6) << "Min" << setw(6) << "Max" << setw(6) << "Average" << endl;
cout << "-------------------------------------------------------" << endl;
//reading student id
infile >> id;
while(infile)
{
//reading four grades
infile >> a1 >> a2 >> a3 >> a4;
//calling the processARow function
ProcessARow(a1, a2, a3, a4, min, max, average);
//displaying the details
// I am modified this printf() statement using cout with input output manipulation<iomanip.h> setw()
cout << left << setw(12) <<id<< setw(6) <<a1<< setw(6) <<a2<< setw(6) <<a3<< setw(6) <<a4<< setw(6) <<min<< setw(6) <<max<< setw(6) <<average<< endl;
//reading student id
infile >> id;
}
system("pause");
return 0;
}
//processARow function
void ProcessARow(int a1, int a2, int a3, int a4, int &min, int &max, float &average)
{
//finding min vaule
if(a1 < a2 && a1 < a3 && a1 < a4)
min = a1;
else if(a2 < a3 && a2 < a4) // add additional condition a2 < a4
min = a2;
else if(a3 < a4)
min = a3;
else
min = a4;
//finding max
if(a1 > a2 && a1 > a3 && a1 > a4)
max = a1;
else if(a2 > a3 && a2 > a4) // add addition condition a2 > a4
max = a2;
else if(a3 > a4)
max = a3;
else
max = a4;
//getting average
average = (a1 + a2 + a3 + a4) / 4.0;
}
OUTPUT:
std-id A1 A2 A3 A4 Min Max Average
-------------------------------------------------------
126534 7 8 10 7 7 10 8
321345 5 6 4 9 4 9 6
324341 8 3 8 5 3 8 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.