patterncppMinor
findLowest() and findHighest() functions in this simple program
Viewed 0 times
thissimpleprogramfindhighestfindlowestandfunctions
Problem
My code does the job for the most part, but I don't think the two functions that look for the highest and lowest score are the best approach. The instructions tell me to use the
A particular talent competition has 5 judges, each of whom awards a
score between 0 and 10 to each performer. Fractional scores are
allowed. The final score of a performer is determined by dropping the
highest and lowest score received, then averaging the 3 remaining
scores. Should include the following functions:
-
-
the performer received. This function can only be called once by
Two additional functions should be called by
the returned info to determine which of the scores to drop:
-
-
```
#include
#include
using namespace std;
// Function prototypes
void getJudgeData(double &someValue);
double calcScore(double a, double b, double c, double d, double e);
double findLowest(double aa, double bb, double cc, double dd, double ee);
double findHighest(double aaa, double bbb, double ccc, double ddd, double eee);
int main ()
{ double scores, avgValue, judge1, judge2, judge3, judge4, judge5;
// Ask for the score of each judge
getJudgeData(scores);
judge1 = scores;
cout > someValue;
if (someValue 10)
{ cout > someValue;
}
else {
someValue;
}
}
/* calcScore calculates an
int as the return instead of double, but I can't tell how to take this approach.A particular talent competition has 5 judges, each of whom awards a
score between 0 and 10 to each performer. Fractional scores are
allowed. The final score of a performer is determined by dropping the
highest and lowest score received, then averaging the 3 remaining
scores. Should include the following functions:
-
void getJudgeData() - should ask the user for the judge's score, store it in a reference parameter variable, and validate it. Called inmain() for each 5 judges.-
double calcScore() - should calculate and return the average of the 3 scores that remain after dropping the highest and lowest scoresthe performer received. This function can only be called once by
main() and should be passed the 5 scores.Two additional functions should be called by
calcScore(), which usesthe returned info to determine which of the scores to drop:
-
int findLowest() - should find and return the lowest of the 5 scores passed to it.-
int findHighest() - should find and return the highest of the 5 scores passed to it.```
#include
#include
using namespace std;
// Function prototypes
void getJudgeData(double &someValue);
double calcScore(double a, double b, double c, double d, double e);
double findLowest(double aa, double bb, double cc, double dd, double ee);
double findHighest(double aaa, double bbb, double ccc, double ddd, double eee);
int main ()
{ double scores, avgValue, judge1, judge2, judge3, judge4, judge5;
// Ask for the score of each judge
getJudgeData(scores);
judge1 = scores;
cout > someValue;
if (someValue 10)
{ cout > someValue;
}
else {
someValue;
}
}
/* calcScore calculates an
Solution
With C++11, some code may be simplified:
But if you use arrays and this algorithm, the code may be simplified even more:
double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
return std::min({aaa, bbb, ccc, ddd, eee});
}
double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
return std::max({aaa, bbb, ccc, ddd, eee});
}But if you use arrays and this algorithm, the code may be simplified even more:
/* calcScore calculates and return the average of 4 scores that remain after
dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
auto it = std::minmax_element(std::begin(a), std::end(a));
return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second)) / 3;
}
int main ()
{
double judges[5];
// Ask for the score of each judge
for (auto& judge : judges) {
getJudgeData(judge);
std::cout << judge << std::endl;
}
//Outputs the average value
avgValue = calcScore(judges);
std::cout << avgValue;
return 0;
}Code Snippets
double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
return std::min({aaa, bbb, ccc, ddd, eee});
}
double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
return std::max({aaa, bbb, ccc, ddd, eee});
}/* calcScore calculates and return the average of 4 scores that remain after
dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
auto it = std::minmax_element(std::begin(a), std::end(a));
return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second)) / 3;
}
int main ()
{
double judges[5];
// Ask for the score of each judge
for (auto& judge : judges) {
getJudgeData(judge);
std::cout << judge << std::endl;
}
//Outputs the average value
avgValue = calcScore(judges);
std::cout << avgValue;
return 0;
}Context
StackExchange Code Review Q#68720, answer score: 6
Revisions (0)
No revisions yet.