Rustlinjimmies
Rustlinjimmies

Reputation: 9

Find the minimum and maximum element of 5 different elements in C++

Need to take out the highest and lowest numbers of 5 judges 0 to 10 on the score

cout << "Please enter the name of the athelete.\n";

            getline(cin, name);


            cin >> name;

            cout << "Please enter the first judge's score below\n";


            cout << "Please enter a score between 0 and 10.\n";

            cin >> First_Score;

Upvotes: 0

Views: 348

Answers (3)

sehe
sehe

Reputation: 392979

Always separate input and logic.

#include <vector>  // std::vector
#include <numeric> // std::sort, std::accumulate
#include <cassert> // assert

auto score(std::vector<double> scores) {
    assert(scores.size()>2);
    auto n = scores.size() - 2;
    // sort ...
    // sum ...
    return sum / n;
}

int main() {
    assert(score({1,5,9}) == 5);
    assert(score({5,5,5}) == 5);
    assert(score({0,5,0}) == 0);
    assert(score({1,2,3,4,5,6}) == 3);
}

I left the actual implementation for you. My implementation fits in three lines.

I hope this inspires you to get a clean implemntation, that you can easily use to write the rest of the program with.

Upvotes: 0

Jugurtha
Jugurtha

Reputation: 123

The simplest solution would be to create two additional variables. The first, 'lowest', to store the lowest value and the second, 'biggest' to store the biggest value.

int lowest = 0, biggest = 11;
...
cin >> First_Score;
            while (First_Score < 0 || First_Score > 10)
            {
                cout << "That is not a valid input, please pick between 0 and 10.\n";
                cin >> First_Score;
                cout << "Your selection was " << First_Score << endl;
            }
lowest = First_Score;
biggest = First_Score;
...

And then for each judge :

 cout << "Please enter the fifth judge's score here below\n";
            cin >> Fifth_Score;

            while (Fifth_Score < 0 || Fifth_Score > 10)
            {
                cout << "That is not a valid input, please pick between 0 and 10.\n";
                cin >> Fifth_Score;
                cout << "Your selection was " << Fifth_Score << endl;
            }
if(lowest > Fifth_Score)
 lowest = Fifth_Score;
if(biggest < Fifth_Score)
 biggest = Fifth_Score;
...

int sum = Avg_Score = (First_Score + Second_Score + Third_Score + Fourth_Score + Fifth_Score);
Avg_Score = (sum - lowest - biggest) / 3;
...

Upvotes: 1

Surt
Surt

Reputation: 16099

Pseudo code

loop for 5 answers
sort them
sum of middle 3 
avg sum / 3
print

Upvotes: 1

Related Questions