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 49 50 51 52 53 54 55 56 57 58 59 60
|
//Star Search
#include <iostream>
using namespace std;
double getScore( double maxScore );
double lowest( double, double, double, double, double );
double highest( double, double, double, double, double );
double average( double, double, double, double, double );
bool isValid( double theScore, double maxValue );
int main()
{
const double MAX_SCORE = 10;
cout << "Please enter the 5 judge scores" << endl;
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 ) << endl;
}
double getScore( double maxScore )
{
double score ;
cout << "Enter the judge scores: ";
cin >> score ;
if( isValid( score, maxScore ) ) return score ;
cout << "Invalid score, 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) ) ) ) ; }
double higher_of( double a, double b ) { return a>b ? a : b ; }
double highest( double s1, double s2, double s3, double s4, double s5 )
{ return higher_of( s1, higher_of( s2, higher_of( s3, higher_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);
const double highest_score = lowest(s1,s2,s3,s4,s5);
return ( s1+s2+s3+s4+s5 - lowest_score - highest_score) / 3 ;
}
| |