1
votes

I am writing code for a C++ problem involving a diving competition. My code compiles and asks the user for 5 scores between 1-10. It also takes the dive difficulty between 1-1.67 and it calculates the total score for each diver and the average per diver.

I am having trouble with isolating the highest and lowest score in a loop that only accumulates the score into totalScore, so I am unable to do a comparison because I do not have (score1, score2, score3, score4, score5). Instead, all of the values are being stored in totalScore and I am not sure how to compare each input with the highest and lowest as they are entered because I am using a loop.

Is there any way to do this inside my loop or another way?

I have already tried to use an if-else statement that compares each score to the current max/min, but my scores are all being saved into one variable. I have tried to set my input score to a long if-else statement, but if the input is (5,7,6,8,9), then when 5 is first processed through the loop it saves to "highest score" because logically it is currently the highest score, but when I run the next number in the loop it will replace 5 because logically 7 is bigger than 5, and 5 is not being put into lowestscore because the loop has already ran and 7 replaced 5.

for (numJudges = 1; numJudges < 6; numJudges++) //Input score from 5 Judges
        {
            cout << "Enter the score given by Judge #" << numJudges << ": ";
            cin >> inputScore;
            while (inputScore < 0 || inputScore > 10) // Validate if the input score is between 0-10
            {
                cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
                cout << "Enter the score given by Judge #" << numJudges << ": ";
                cin >> inputScore;
            }
            totalScore += inputScore;//Add score to total
        }
        cout << "What was the degree of difficulty? "; //Input degree of difficulty
        cin >> diveDifficulty;
        while (1 > diveDifficulty || diveDifficulty > 1.67) // Validate degree of difficulty
        {
            cout << "Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)\n";
            cout << "What was the degree of difficulty? ";
            cin >> diveDifficulty;
        }

        diverTotal = totalScore - highScore - lowScore;
        overallScore = (diverTotal / 3) * diveDifficulty;
        totalScore = saveTotal;

For example, Input:

Judge1: 8, Judge2: 8, Judge3: 8, Judge4: 7, Judge5: 9,

Dive difficulty: 1

The overall score should simply be 8 by adding only the 8's and dropping the 7 and 9 then dividing by the 3 remaining scores and multiplying by 1; however, whenever I would use a comparison if-else statement (under '//add score to total'), it does not correctly compare the current input to the running high/low score. I am not sure if it is even possible having only one input variable where all the scores are stored. I think I have to use a loop within the loop but I am not sure how to set this up.

2
std::minmax_element should be helpful. - user4581301

2 Answers

2
votes
int max = 0

When a user enters a number, or a judge outputs a score, just compare that number with max. If the number is greater than max, then have max = the output.

Continuing doing this and you are guaranteed to have the maximum value at the end of your input loop.

Same concept applies for a min. Just have int min = INT_MAX

0
votes

What you need to do is keep track of the min and max during the loop when you get the inputs.

// Initialize these before the first loop...
double highScore = 0.0;
double lowScore = 10.0;
...

...
while (inputScore < 0 || inputScore > 10) // Validate if the input score is between 0-10
{
   cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
   cout << "Enter the score given by Judge #" << numJudges << ": ";
   cin >> inputScore;
}
highScore = std::max(inputScore, highScore);
lowScore = std::min(inputScore, lowScore);
totalScore += inputScore;
...