Thread: Minimum/maximum number problem. Please help!

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Step 2 TOTALLY messes up the program. Say I input 76 and 8. It puts the max number to 0 and the minimum to 8.
    I think this can be fixed by removing the "else" in this statement:

    Code:
                if (num < min)
                num = min;
                else if (num > max)
                num = max;

  2. #17
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by AndrewHunter View Post
    Post your updated code and note that the code that hash posted still has the assignments backwards.
    sorry my mistake..

  3. #18
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Have you learned if statements? So something like:

    while num isn't 0
    if num isn't 0
    do all your code
    loop
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #19
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Post your updated code and note that the code that hash posted still has the assignments backwards.
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    int main (void)
    
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list. Signal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          min = 0;
          max = 0;
          while (num!=0)
          {
                if (num < min)
                min = num;
                else if (num > max)
                max = num;
                else num = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("The maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }

  5. #20
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Have you learned if statements? So something like:

    while num isn't 0
    if num isn't 0
    do all your code
    loop
    Yes, I have learned if statements. The rest of what you said doesn't make sense to me. Haven't I already done that?

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If your input is never going to be zero, you can get away with doing this (though it's sloppy):

    Code:
        if ((num < min)&&(num != 0)
    This will not let the value of "min" be updated if the input is zero.

  7. #22
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by Matticus View Post
    I think this can be fixed by removing the "else" in this statement:

    Code:
                if (num < min)
                num = min;
                else if (num > max)
                num = max;
    That didn't change anything.

  8. #23
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Let me repost the ops original code block for all you "do while()er's" out there...

    Code:
          num = GetInteger();
          min = 0;
          max = 0;
          while (num!=0)
          {
                if (num < min)
                num = min;
                else if (num > max)
                num = max;
                else num = num; /* btw  what is this line about? dont even have an ELSE if you dont need one */
                printf ("Enter your next number: ");
                num = GetInteger();  /* whats the next statement after this executed? */
                }
    So if GetInteger is 0 it will exit, the real problem is that he is starting out with min = 0, so of course nothing will be LESS then zero, that is the whole purpose of "step 2" originally posted by subhash

    The only missing problem is that the logic needs to change from "if else if" to just "if and if" ... the max should always be set despite the fact the min was set
    Last edited by nonpuz; 07-14-2011 at 11:27 PM. Reason: Forgot to make clear that assignments are still backwards (and added comment)

  9. #24
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by PYROMANIAC702 View Post
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    int main (void)
    
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list. Signal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          min = 0;
          max = 0;
          while (num!=0)
          { 
               if (num < min)
                     min = num;
                else if (num > max) //NOT NEEDED
                     max = num;
                else 
                     num = num; //NOT NEEDED
                printf ("Enter your next number: ");
                num = GetInteger();
         }
         printf ("The maximum number in the list is %d.\n", max);
         printf ("The minimum number in the list is %d.\n", min);
         getchar();
    }
    You don't need that final else.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #25
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That didn't change anything.
    It does if the first value you enter is the largest, assuming you assigned "min" to an initial value much greater than zero.

    EDIT: nonpuz summed this up first above.

  11. #26
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by Matticus View Post
    If your input is never going to be zero, you can get away with doing this (though it's sloppy):

    Code:
        if ((num < min)&&(num != 0)
    This will not let the value of "min" be updated if the input is zero.
    No, it will not, but that is at the top of the loop so if the last number i type is 0, then it will disregard that.

  12. #27
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by nonpuz View Post
    Let me repost the ops original code block for all you "do while()er's" out there...

    Code:
          num = GetInteger();
          min = 0;
          max = 0;
          while (num!=0)
          {
                if (num < min)
                num = min;
                else if (num > max)
                num = max;
                else num = num; /* btw  what is this line about? dont even have an ELSE if you dont need one */
                printf ("Enter your next number: ");
                num = GetInteger();  /* whats the next statement after this executed? */
                }
    So if GetInteger is 0 it will exit, the real problem is that he is starting out with min = 0, so of course nothing will be LESS then zero, that is the whole purpose of "step 2" originally posted by subhash

    The only missing problem is that the logic needs to change from "if else if" to just "if and if" ... the max should always be set despite the fact the min was set
    Thanks! That worked. Thanks to everybody else for your time and effort. Before when i tried it it was min = 9999 and it was if else if. With that combination it did not work, but when I changed it to if if it worked.

    EDIT: I used min != 0, not min = 9999. It looks and works much better.

    Final Code:

    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    int main (void)
    
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          min != 0;
          max = 0;
          while (num!=0)
          {
                if (num < min)
                min = num;
                if (num > max)
                max = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("The maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }
    Last edited by PYROMANIAC702; 07-14-2011 at 11:35 PM.

  13. #28
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    min != 0; is not an assignment operation its a conditional statement, resulting in NOTHING in this case. What really happened is that your "min" is now initialized to some random data (by compiler) since you didn't set it specifically equal to zero
    Last edited by nonpuz; 07-14-2011 at 11:39 PM. Reason: misspelled "now" as not

  14. #29
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by nonpuz View Post
    min != 0; is not an assignment operation its a conditional statement, resulting in NOTHING in this case. What really happened is that your "min" is now initialized to some random data (by compiler) since you didn't set it specifically equal to zero
    Yes, but with 9999 it would make 9999 the biggest value the user could enter. != 0 is infinite.

  15. #30
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so the easiest way to do this would be:
    Code:
    num = GetInteger();
    max=min=num;
    while.....
    And != does not work as an assignment operator.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding mean minimum and maximum values
    By begginer in forum C Programming
    Replies: 1
    Last Post: 03-25-2011, 11:55 PM
  2. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 03:54 PM
  3. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  4. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM
  5. Maximum And Minimum
    By drdodirty2002 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 12:39 AM