An unprecedented n00b thread...

This is a discussion on An unprecedented n00b thread... within the C Programming forums, part of the General Programming Boards category; Omg can someone please inform me on how I can sort 3 integers only with basic commands like printf, scanf, ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Talking An unprecedented n00b thread...

    Omg can someone please inform me on how I can sort 3 integers only with basic commands like printf, scanf, and if statements? No looping or switches.

    I tried heaps of things, creating high and low integers but this is what I've got at the moment:


    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
    
    int a, b, c;
    
     printf("Please enter 3 integers: ");
     scanf("%d, %d, %d", &a, &b, &c);
    
    //Determine highest
    
     if ( (a > b) && (a > c) && (b > c ) ) {                   /* for a, b, c */
     printf(" Your numbers in order are: %d %d %d\n ", a, b, c); 
     }
     if ( (b > a) && (b > c) && (c < a) ) {                  /* for b, a, c */
     printf(" Your numbers in order are: %d %d %d\n ", b, a, c);
     }
     if ( (a > c) && (a > b) && (c > b) ) {
     printf(" Your numbers in order are %d %d %d\n", a, c, b); /* for a, c, b */ 
     }
     if ( (b > a) && (b > c) && (c > a) ) {
     printf("Your numbers in order are %d %d %d\n", b, c, a); /* for b, c, a */
     }
     if ( (c > a) && (c > b) && (a > b) ) {
     printf("Your numbers in order are %d %d %d\n", c, a, b);  /* for c, a, b */
     }
     if ( (c > a) && (c > b) && (b > a) ) {
     printf("Your numbers in order are %d %d %d\n", c, b, a);  /*  for c, b, a */
     }
     
     return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is a bit excessively complicated. Assuming you don't actually need to keep a, b and c with their current values, you can use the "sorting approah":
    if (b > a) swap a with b;
    if (b > c) swap b with c;
    if (a > c) swap a with c;
    Now a > b > c

    Swapping is done by storing the first value in a temporary variable, then storing the second in the first, finally storing temp back in the second one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Won''t work.

    Try 3 5 4.

    If b > a, swap a with b: yields 5 3 4
    If b > c, swap b with c: yields 5 3 4
    If a > c, swap a with c: yields 4 3 5

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Try this:

    if b < a, swap a with b
    if c < a, swap a with c
    if c < b, swap b with c

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doh!

    so:
    if (a > c) swap(a, c);
    if (b > c) swap(b, c);
    if (a > b) swap(a, b);

    I just tested that with varying inputs, and as far as I can tell, it works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Since you're not allowed to use loops, I'd use a array -- move the #'s as required,

    Run it n - 1 times. For example,
    Code:
    void premote_three(int * numbers)
    {
        int temp = 0;
        
        if(numbers[2] > numbers[1])
        {
            temp = numbers[1];
            numbers[1] = numbers[2];
            numbers[2] = temp;
        }
    
        if(numbers[1] > numbers[0])
        {
            temp = numbers[0];
            numbers[0] = numbers[1];
            numbers[1] = temp;
        }
        return;
    }
    First run, on 3, 5, 4: 5, 3, 4
    Second run (on 5, 3, 4): 5, 4, 3

    Ugly but it works... of course it doesn't have to be an array (but if they then said -- "Do the same thing using loops", you'd already be ahead )
    Last edited by zacs7; 03-27-2008 at 06:30 AM.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Thanks for the help. But I should have stressed more that I'm trying to just use If statements. To simplify it can I create a new integer or float or something?
    The output I get with my original code that I posted seems to be two random large numbers and the value for 'a' in between.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by gman89 View Post
    Thanks for the help. But I should have stressed more that I'm trying to just use If statements. To simplify it can I create a new integer or float or something?
    The output I get with my original code that I posted seems to be two random large numbers and the value for 'a' in between.
    You're probably not entering data as you've told scanf to expect: number comma space number comma space number.

    Now, ask yourself what happens with your program when you enter: 3, 4, 3

    Also, lets have a go at simplifying your code. First thing to notice is that the first 3 if statements all include a test for (a > c) or (c < a) which are the same thing. So those first three can be grouped inside an if statement that only does that check once:
    Code:
    if (a > c) {
        if ( (a > b) && (b > c) ) {                  /* for a, b, c */
            printf("Your numbers in order are: &#37;d %d %d\n", a, b, c);
        }
        if ( (b > a) && (b > c) ) {                  /* for b, a, c */
            printf("Your numbers in order are: %d %d %d\n", b, a, c);
        }
        if ( (a > b) && (c > b) ) {                  /* for a, c, b */
            printf(" Your numbers in order are %d %d %d\n", a, c, b);
        }
    }
    That's step one. Now can you apply the same kind of simplification again inside that? What about applying the same kind of simplification to the other 3 existing if-statements too?
    Last edited by iMalc; 03-28-2008 at 01:37 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Hey thanks, that actually worked when I entered integers with comma's in between. I never would have picked that up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 04:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 09:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21