Finding min & max numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BoscoPippa
    New Member
    • Oct 2007
    • 6

    Finding min & max numbers

    Hi all,

    I'm a rank beginner to C++ and programming in general. I'm in week 6 of my first course, and we have an assignment I'm having a little trouble with. If it matters, we're using standard (?) C++ in our class, not ANSI/ISO.

    Anyway....

    The problem is to do the following:
    1. Ask for input of up to 10 numbers
    2. Return the number of entries, the total, the average, the minimum and maximum values
    3. Allow user to enter a sentinel of 9999 to end the program.

    The part I'm having trouble with is the minimum/maximum. Here's part of the code I have so far:

    Code:
       double num, total = 0, avg, min, max;
       int count = 0;
    
       min = num;
       max = num;
       
       while ( count < 10 )
       {
          cout << "Enter value #" << count + 1 << ": ";
          cin >> num;
    
          if ( num != SENTINEL )        // Check for the SENTINEL value 9999
          {
             total += num;                  // total = total + num
             count++;                        // Raise count by 1
          }
          else
             break;                           // End program if SENTINEL is entered.
    
       // Evaluate min and max.  
          if ( min > num )
             min = num;                 // Change min only if num is lower
          else if ( max < num )
             max = num;                 // Change max only if num is higher
       }
    If I put the min = num; max = num; part inside the while loop, min and max are reset every time the loop repeats. If I put it outside the loop, I keep getting a return of 0 for min.

    Can someone please nudge me in the right direction? Thank you so much!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Lines 4 and 5 are unnecessary, as double defaults to 0.0 anyway.

    As to your problem with min, you set it to 0.0 then only allow it to be changed if the input is less than that. You probably want to initialize to DOUBLE_MAX, which is C++'s maximum possible value for a double.

    Basically, min should be initialized to something greater than any possible input and max should be initialized to something less than any possible input. Or you could use an array, if you've learned how to use them.

    Comment

    • BoscoPippa
      New Member
      • Oct 2007
      • 6

      #3
      Thanks, Laharl! I had forgotten that double defaulted to 0. I didn't take as good notes last week as I usually do, so I don't remember exactly how the prof did it. I had thought he had assigned num to min and max, but I obviously got that wrong! He didn't use DOUBLE_MAX, though, that much I do remember.

      We haven't learned arrays yet. We're moving on to functions next. :-)

      Originally posted by Laharl
      Lines 4 and 5 are unnecessary, as double defaults to 0.0 anyway.

      As to your problem with min, you set it to 0.0 then only allow it to be changed if the input is less than that. You probably want to initialize to DOUBLE_MAX, which is C++'s maximum possible value for a double.

      Basically, min should be initialized to something greater than any possible input and max should be initialized to something less than any possible input. Or you could use an array, if you've learned how to use them.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Using DOUBLE_MAX is certainly one way to do it, but it's kind of messy. Your professor probably assigned min and max to the first number entered, and then proceeded with the loop, which is the approach I would take. So you did see "min = num; max = num;" but must have missed the separation between the first input and the looped input.

        Comment

        • BoscoPippa
          New Member
          • Oct 2007
          • 6

          #5
          I was thinking that's what he did. I was trying to avoid having a separate cin for the first entry, but I couldn't figure out how that could be done.

          (coming back to this reply 30 min. later...)
          I got it! I wish I could say I figured it out logically, but mostly I got it to work by moving parts around before and after the SENTINEL, and inside and outside of the loop. I put a test for the first entry (if (count==0)) before the SENTINEL, and the min/max comparisons right after it.

          Code:
             while ( count < 10 )
             {
                cout << "Enter value #" << count + 1 << ": ";
                cin  >> num;
          
                if ( count == 0 )              // Set initial values of min and max
                {
                   min = num;
                   max = num;
                }
                  
                if ( num != SENTINEL )        // Check for the SENTINEL value 9999
                {
                   total += num;              // total = total + num
                   count++;                   // Raise count by 1
                }
                else
                   break;                     // End program if SENTINEL is entered.  
          
                if ( min > num )
                   min = num;                 // Change min only if num is lower
                else if ( max < num )
                   max = num;                 // Change max only if num is higher 
              }
          Thanks, Ganon11, for your help!

          Originally posted by Ganon11
          Your professor probably assigned min and max to the first number entered, and then proceeded with the loop, which is the approach I would take. So you did see "min = num; max = num;" but must have missed the separation between the first input and the looped input.

          Comment

          Working...