In: Computer Science
USING C++ PLEASE
Write a program named Lab11C that calculates the average of a group of test scores where the lowest score is dropped. It should use the following functions
a) void getScores should have 4 double reference
parameters (one for each test score)
getScores should do the following:
- read 4 test scores from the text file Lab11C.txt
(Important: declare your ifstream infile and open the file inside
this function, not in the main function)
- store them in the reference parameters
b) void calcAverage (4 double parameters) should do the following:
- call findLowest passing the test scores as parameters
- total the 4 scores and subtract the result returned by findLowest
- this will leave you with the total of 3 test scores, so divide the total by 3
- print the average with a label
c) double findLowest (so it has 4 double parameters) should find and return the lowest of the 4 scores passed to it.
d) In the main function
- call getScores sending 4 double variables as parameters
- call calcAverage sending those 4 double variables
Note about reference parameters: remember that with reference parameters, you will use the & in the top line of the function itself, but you will not use the & in the call to the function in main.
Main Code :
#include<iostream>
#include<fstream>
using namespace std;
void getScores(double &a,double &b,double &c,double
&d)
{
ifstream f;
f.open("Lab11C.txt");
if(!f)
cout<<"Unable to open the file";
else
{
f>>a;
f>>b;
f>>c;
f>>d;
}
f.close();
}
double findLowest(double a,double b,double c,double d)
{
if((a<b)&&(a<c)&&(a<d))
return a;
else if((b<c)&&(b<d))
return b;
else if(c<d)
return c;
else
return d;
}
void calcAverage(double a,double b,double c,double d)
{
double low=findLowest(a,b,c,d);
double tot=a+b+c+d;
tot=(tot-low)/3;
cout<<"Average = "<<tot;
}
int main()
{
double a,b,c,d;
getScores(a,b,c,d);
calcAverage(a,b,c,d);
}
File : Lab11C.txt
Output :