This is my assignment: Write a program that reads test scores (four scores for e
ID: 3627228 • Letter: T
Question
This is my assignment:Write a program that reads test scores (four scores for each student) from a file. The program should use value-returning functions to determine the test average. (The test average is the average of the three highest test scores.). The program should use priming read and a while loop that terminates when there is no more data in the file. The program should output the four test scores rounded to one decimal place and the average rounded to two decimal places to a file.
I made most of the program but I don't know how to find the three highest test score to calculate the average.
This is what I have done so far:
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;
double FindAverage(double, double, double, double);
int main()
{
ifstream inData;
ofstream outData;
double score1;
double score2;
double score3;
double score4;
double average;
inData.open("tests.txt");
outData.open("tests-output.txt");
cout << "Data is being processed" << endl;
outData << setw(15) << "Test 1" << setw(25) << "Test 2" << setw(30) << "Test 3" << setw(30) << "Test 4" << setw(30) << "Average" << endl;
inData >> score1;
inData >> score2;
inData >> score3;
inData >> score4;
while (inData)
{
average = FindAverage(score1, score2, score3, score4);
outData << fixed << showpoint << setprecision(2);
outData << setw(15) << score1;
outData << setw(25) << score2;
outData << setw(30) << score3;
outData << setw(30) << score4;
outData << setw(30) << average;
inData >> score1;
inData >> score2;
inData >> score3;
inData >> score4;
}
inData.close();
outData.close();
getch();
return 0;
}
double FindAverage(double score1, double score2, double score3, double score4)
{
return (score1 + score2 + score3 + score4) / 4;
}
Please help.
Explanation / Answer
Hi Danio13, To find the highest of the 3 scores you can look for the lowest of the 4 and subtract that from the cumulative score. Here is a sample solution and the simplest one to me. -B rewrite your FindAverage() function to: double FindAverage(double score1, double score2, double score3, double score4) { double lowest = score1 if (lowest > score2) lowest = score2; if (lowest > score3) lowest = score3; if (lowest > score4) lowest = score4; return (score1 + score2 + score3 + score4 - lowest) / 3; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.