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

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    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. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    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.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    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. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by nobletype View Post
    I'm trying to follow.
    Try answering the questions, perhaps then the answer will become clear to you.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    I tried. Honestly I'm just not understanding it. If you could be a bit more informative I'd greatly appreciate it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    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. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    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. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by nobletype View Post
    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?
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    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. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by nobletype View Post
    It's from a totally new batch of cards.
    Of course it is, but why did you choose 6, and not say, 0?
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    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. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    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
    Last edited by MWAAAHAAA; 10-26-2010 at 12:02 PM.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  4. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM