1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
using namespace std;
double getScore( double maxScore );
double lowest( double, double, double, double, double );
double average( double, double, double, double, double );
bool isValid( double theScore, double maxValue ) ;
int main()
{
const double MAX_SCORE = 100 ;
cout << "Please enter the 5 test scores \n";
const double s1 = getScore(MAX_SCORE) ;
const double s2 = getScore(MAX_SCORE) ;
const double s3 = getScore(MAX_SCORE) ;
const double s4 = getScore(MAX_SCORE) ;
const double s5 = getScore(MAX_SCORE) ;
cout << "average of the four higher scores: "
<< average( s1, s2, s3, s4, s5 ) << '\n' ;
}
double getScore( double maxScore )
{
double score ;
cout << "Enter a test score: ";
cin >> score ; // we assume hat the user enters a number
if( isValid( score, maxScore ) ) return score ;
cout << "Invalid number, please enter a new one.";
return getScore(maxScore) ;
}
double lower_of( double a, double b ) { return a<b ? a : b ; }
double lowest( double s1, double s2, double s3, double s4, double s5 )
{ return lower_of( s1, lower_of( s2, lower_of( s3, lower_of(s4,s5) ) ) ) ; }
bool isValid( double theScore, double maxValue )
{ return theScore >= 0 && theScore <= maxValue ; }
double average( double s1, double s2, double s3, double s4, double s5 )
{
const double lowest_score = lowest(s1,s2,s3,s4,s5) ;
return ( s1+s2+s3+s4+s5 - lowest_score ) / 4 ;
}
|