# Thread: Needing a little bit of help with finding the minimum value

1. ## Needing a little bit of help with finding the minimum value

For one of my intro to C++ assignments, I must write code where a group of athletes performances is judged by a variable number of judges. The performance score of the athletes is obtained by rejecting both the lowest and the highest score, and taking the average of the remaining scores. I have most of it figured out I think, however the minimum score is a bit over my head; the max score can be reset to 0, for following athletes to be judged, but the minimum cannot, because no score will be less than 0 to be picked up as a low score. My code:

Code:
```#include <iostream>
using namespace std;
int main ()
{
int athleteid, numjudges, startjudges, scorecard;
double score, total, average, min, min2, max=0;
cout.setf(ios::fixed,ios::floatfield);
cout.precision (1);
cout << "Welcome to the athlete performance score program" << endl;
cout << "To start, please enter the athlete ID> ";
cin >> athleteid;
while (athleteid > 0)
{
scorecard=1;
cout << "How many judges gave entered a score?> ";
cin >> numjudges;
for (startjudges=1; startjudges <= numjudges; startjudges++)
{
cout << "Please enter score " << scorecard << ">";
cin >> score;
if (score > max)
{
max = score;
}
if (score < min)
{
min = score;
}
total = total+score;
scorecard++;
}
average= (total-(max+min))/(numjudges-2);
cout << "The average is " << average << endl;
total=0;
max=0;
min=(I get stuck here);
cout << "Finished. Enter next athlete ID, 0 to finish> ";
cin >> athleteid;
}
system("pause");
}```
Everything works out well for the most part, but I have no clue on how to reset the min variable, to pick up the lowest number again while the loop goes back around.

Any help would be appreciated.

2. 6, 8, 2, 9, 1, 9, 6

Which is the smallest number? How did you figure that out? Did you need to compare with 0 in order to do so?

Work it out on paper, then translate to code.

3. I'm trying to follow.

My min variable works the first time around, and I did not have to compare it to 0. However, when the loop comes back around again to judge the next athlete, the min integer is still holding a value from a previous judged athlete. My problem is resetting min for the next loop. Resetting it to 0 won't help like it would for total, and max, because it does the exact opposite.

4. Originally Posted by nobletype
I'm trying to follow.
Try answering the questions, perhaps then the answer will become clear to you.

5. I tried. Honestly I'm just not understanding it. If you could be a bit more informative I'd greatly appreciate it.

6. Describe your thought process in answering MWAAAHAAA's question of picking the smallest number among 6, 8, 2, 9, 1, 9, 6. In fact, you should do this away from your computer, without any thought of programming. Select some cards from a deck of cards, if you will, and go through the exercise physically, taking care to be very methodical, considering one card at a time.

7. Okay. 6 is less than 8. 6 is not less than 2, thus 2 is the newest low number. 2 is less than 9. 2 is not lower than 1, thus 1 is the newest low number. 1 is less than 9. 1 is less than 6.

Now lets say I have a new deck of cards, where my mind is supposed to keep a clean slate in terms of the lowest number. But I can't shake it off. Now, the deck of cards is 4, 2, 6, 7, 10, 3. I won't be able to pick a low number from these because I can't shake off the 1 as the lowest number from my last batch of cards. Thus, I accidentally pick 1 as the lowest number, even if it wasn't part of this new batch of cards.

See the problem?

8. Well, I feel kind of stupid, now. I just set the min afterward to 11, as the range of input numbers will always be less than that. How did I somehow miss that? Sheesh.

I don't know if that's what you two were getting to, but I suppose that's one way to fix it.

Thanks for the input, guys.

9. Originally Posted by nobletype
I just set the min afterward to 11, as the range of input numbers will always be less than that. How did I somehow miss that?
Yes, that is another way to solve the problem.

10. Originally Posted by nobletype
I don't know if that's what you two were getting to, but I suppose that's one way to fix it.
While it is indeed a way to fix it, it was not what I was getting at. Look at what you wrote:

Okay. 6 is less than 8.
Where did that 6 come from? Was it from your last batch of cards?

11. Nope. It's from a totally new batch of cards. Are you saying I should apply a new variable for min for each new batch of cards? Because I do not know how that's done within a loop.

12. Originally Posted by nobletype
It's from a totally new batch of cards.
Of course it is, but why did you choose 6, and not say, 0?

13. Because 0 was never part of the batch to begin with. I always knew that, that's why I knew it wouldn't work to be reset to that. 0 would always be the lowest number. I just brought 0 up because that's how it works to reset integers max, and total. I'm still trying to understand what you're getting at.

14. I'm still trying to understand what you're getting at.
I'm not trying to annoy you, the solution you managed to find actually works, although it is not really robust. I am just trying to get you to figure out the alternative for yourself. And to be completely fair with you, you'd have to actually rewrite your algorithm slightly (or more to the point, you'd have to include an additional step right in the middle) in order to be able to do what I (and I imagine laserlight as well) had in mind. I imagine you are simply to locked on your current solution, to consider the alternative.

So, get your mind off the problem, do something else, and if you're interested enough in it, you may figure it out tomorrow. I'd hate to simply spell the solution out for you

15. Aww. Sometimes it's good to learn from others if someone is stuck in the mud

I was looking forward to see what you had. But, if that's how it's going to be, then so be it.

Thanks, truly.