Thread: The typical Largest smallest number printing

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22

    The typical Largest smallest number printing

    HI! I am a first year IT student and am facing a problem. I need to create a program that finds and prints the largest and smallest number of a series of 5 numbers inputted by the user. I already know how to use if, if-else, &&, and I could've used those but the challenge was to use only the single selection if statement so I had this program made:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int a, b, c, d, e, min, max;
        
        // Ask values from the user and put values to min and max
        printf("Enter value of A: ");
        scanf("%d", &a);
        if(a<min)
        min = a;
        if(a>max)
        max = a;
        
        printf("Enter value of B: ");
        scanf("%d",&b);
        if(b<min)
        min = b;
        if(b>max)
        max = b;
        
        printf("Enter value of C: ");
        scanf("%d", &c);
        if(c<min)
        min = c;
        if(c>max)
        max = c;
        
        printf("Enter value of D: ");
        scanf("%d", &d);
        if(d<min)
        min = d;
        if(d>max)
        max = d;
        
        printf("Enter value of E: ");
        scanf("%d", &e);
        if(e<min)
        min = e;
        if(e>max)
        max = e;
        
        
        printf("Largest is: %d\n", max);
        printf("Smallest is: %d\n", min);
           
        getche();
    }
    but it seems to be missing something(probably) as it outputs a wrong max value. I would really appreciate if you could help me with it.

    Thanks,

    jrpet

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    And the reason why you are not using a single variable to read input in and compare with min and max in a nice loop is?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    One of the obvious issues is that you haven't initialised min and max before using them in a comparison.

    Code:
    if(a<min) //what is the value of min at this point?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    You should set min and max to zero. Otherwise they may contain garbage that mess up things.
    Code:
    int a, b, c, d, e, min = 0, max = 0;

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Thank you very much! It was dumb of me not to notice that. Thanks a lot guys!! ^^

    @caludiu: well, I can't really use loops though, I can only use if statements and nothing else,, hehe Thanks though

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Fader_Berg View Post
    You should set min and max to zero. Otherwise they may contain garbage that mess up things.
    What happens if someone enters in all positive integers?


    [EDIT]

    The second half of your statement is spot on though.

    [EDITEDIT]

    ...or all negative integers.
    Last edited by bivhitscar; 06-22-2011 at 07:15 AM. Reason: Clarification
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually he should include limits.h and preset min to MAX_INT and max to MIN_INT ... that guarantees that the first comparison will set an initial value.

    Of course since this is not being done in a loop he could just do this...
    Code:
        printf("Enter value of A: ");
        scanf("%d", &a);
        min = a;
        max = a;
    ... and use conditionals in the rest of the chain.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Btw, I solved the prob, I just have to initialize max and I did not initialize min as it will make min = 0 the smallest number thus cannot be changed with the users input.

    Thanks all!!

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Jannr, read common's code - you must initialise both max and min. If you don't initialise a variable before you use it, the behaviour will be undefined.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #10
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Thanks! I've solved it!

    Here it is:

    [Edit]

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int a=0, b=0, c=0, d=0, e=0, min=0, max=0;
        
        // Ask values from the user and put values to min and max
        printf("Enter value of A: ");
        scanf("%d", &a);
        min = a;
        max = a;
        if(a<min)
        min = a;
        if(a>max)
        max = a;
        
        printf("Enter value of B: ");
        scanf("%d",&b);
        if(b<min)
        min = b;
        if(b>max)
        max = b;
        
        printf("Enter value of C: ");
        scanf("%d", &c);
        if(c<min)
        min = c;
        if(c>max)
        max = c;
        
        printf("Enter value of D: ");
        scanf("%d", &d);
        if(d<min)
        min = d;
        if(d>max)
        max = d;
        
        printf("Enter value of E: ");
        scanf("%d", &e);
        if(e<min)
        min = e;
        if(e>max)
        max = e;
        
        
        printf("Largest is: %d\n", max);
        printf("Smallest is: %d\n", min);
           
        getche();
    }
    Last edited by jannr; 06-22-2011 at 07:35 AM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jannr View Post
    Btw, I solved the prob, I just have to initialize max and I did not initialize min as it will make min = 0 the smallest number thus cannot be changed with the users input.

    Thanks all!!
    Wrong... C does not initialize the values of variables declared inside a function (and main() is a function). Without initialization min and max will point to a memory location that will contain whatever garbage is left there by previous function calls. This is what they call "undefined behavior" and it is not to be trusted... It's the kind of thing that works well enough until the teacher is grading your papers, at which point you can be sure it will mess up... In the workplace the results can be catastrophic...

    You should always manually initialize all variables to safe values before use.

    Also... the correct form of main is... int main (void) ... and at the end it has to return a value (usually 0) to the operating system.
    Last edited by CommonTater; 06-22-2011 at 07:22 AM.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    yes but if I also initialize min, the smallest will always be 0 if the user do not enter a negative number.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    AH! I get it,, ok2,, so i just have to initialize both min and max with the first value! Thanks!!

  14. #14
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Quote Originally Posted by bivhitscar View Post
    What happens if someone enters in all positive integers?


    [EDIT]

    The second half of your statement is spot on though.

    [EDITEDIT]

    ...or all negative integers.
    I don't get it... What happens?

    [EDIT]

    Ahhh...
    Last edited by Fader_Berg; 06-22-2011 at 07:26 AM. Reason: * Pling *

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jannr View Post
    yes but if I also initialize min, the smallest will always be 0 if the user do not enter a negative number.
    Read what I suggested...
    Code:
    #include <limits.h>
    
    // inside main
    int a = 0, b = 0, c = 0, d = 0, e = 0, min = INT_MAX, max = INT_MIN;
    Look closely... everything is set to 0 so that should one of your scanf() calls fail (eg, user just presses enter) the garbage values they hold while uninitialized won't affect the outcome. Also min is preset to the highest number possible in an int and max is preset to the lowest this will force your first if statement to assign a value.

    It is always a mistake to trust C to do anything for you... It's a complete idiot, you gotta tell it everything step by step.
    Last edited by CommonTater; 06-22-2011 at 10:55 AM. Reason: fixed constants...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting largest and smallest number
    By .C-Man. in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2011, 06:29 AM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  4. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  5. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM

Tags for this Thread