Thread: Trying to sort 3 numbers from lowest to highest, but I get weird answer

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    23

    Post Trying to sort 3 numbers from lowest to highest, but I get weird answer

    Code:
    #include <stdio.h>
    int main (void)
    {
      int a, b, c, low, med, high;
       printf("***SORT THE  NUMBER***\n");
      // ask user for a, b, c
        printf(" Enter a:\n ");
         scanf("%d", &a);
          printf(" Enter b:\n");
           scanf("%d", &b);
          printf(" Enter c:\n");
         scanf("%d", &c);
    a = high;
    b = med;
    c = low;
    if (a<c<b)
    b = high;
    c = med;
    a = low;
    if (c<a<b)
    c = low;
    a = med;
    b = high;
    
    c = high;
    b = low;
    a = med;
    printf("the sorted numbers are %d, %d, %d\n", a, b, c);
    return 0;
    }
    when i compile it, and input the values, like a=4 b 5 c=2
    i get 134514169, -1218548731, -1217273868

    how do I make the out put as (2 4 5) ?
    what am i doing wrong?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The values of a,b,c are lost when you say "a=high", "b=med" and "c=low"

    It should be "high=a"...

    a<c<b should be a<c&&c<b - you can't do 2 comparisons at the same time.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, you need to use curly braces with your "if" statement, or else only the next line is conditionally executed by the statement
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    23
    Okay so I changed the coding to this
    Code:
     #include <stdio.h>
    int main (void)
    {
      int a, b, c, low, med, high;
       printf("***SORT THE  NUMBER***\n");
      // ask user for a, b, c
        printf(" Enter a:\n ");
         scanf("%d", &a);
          printf(" Enter b:\n");
           scanf("%d", &b);
          printf(" Enter c:\n");
         scanf("%d", &c);
    high = a;
    med = b;
    low = c;
    if (a<c&&c<b)
    {high = b;
    med = c;
    low = a;}
    if (c<a&&a<b)
    {low = c;
    med = a;
    high = b;}
    if (b<a&&a<c)
    {high = c;
    low = b;
    med = a;}
    printf("the sorted numbers are %d, %d, %d\n", a, b, c);
    return 0;
    }
    and whatever number I put for a, b, c, it doesn't sort them

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to format your code properly with good indentation, e.g.,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a, b, c, low, med, high;
        printf("***SORT THE  NUMBER***\n");
    
        // ask user for a, b, c
        printf(" Enter a:\n ");
        scanf("%d", &a);
        printf(" Enter b:\n");
        scanf("%d", &b);
        printf(" Enter c:\n");
        scanf("%d", &c);
    
        high = a;
        med = b;
        low = c;
        if (a < c && c < b)
        {
            high = b;
            med = c;
            low = a;
        }
        if (c < a && a < b)
        {
            low = c;
            med = a;
            high = b;
        }
        if (b < a && a < c)
        {
            high = c;
            low = b;
            med = a;
        }
        printf("the sorted numbers are %d, %d, %d\n", a, b, c);
        return 0;
    }
    Anyway, I notice that at the end, you print a, b and c, rather than high, med and low. Also, I believe that there are 6 ways by which 3 numbers can be ordered, but you only cater to 4.
    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

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    printf("the sorted numbers are %d, %d, %d\n", high, med, low);

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    23
    Quote Originally Posted by camel-man View Post
    Code:
    printf("the sorted numbers are %d, %d, %d\n", high, med, low);
    yea, it's the same. I tried that before and it didn't work.
    I'm very confused why it's not working

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Take into account what Laserlight has said, she points out that you are only accounting for 4 of the 6 cases of ordering.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    23

    Cool

    Quote Originally Posted by laserlight View Post
    You need to format your code properly with good indentation, e.g.,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a, b, c, low, med, high;
        printf("***SORT THE  NUMBER***\n");
    
        // ask user for a, b, c
        printf(" Enter a:\n ");
        scanf("%d", &a);
        printf(" Enter b:\n");
        scanf("%d", &b);
        printf(" Enter c:\n");
        scanf("%d", &c);
    
        high = a;
        med = b;
        low = c;
        if (a < c && c < b)
        {
            high = b;
            med = c;
            low = a;
        }
        if (c < a && a < b)
        {
            low = c;
            med = a;
            high = b;
        }
        if (b < a && a < c)
        {
            high = c;
            low = b;
            med = a;
        }
        printf("the sorted numbers are %d, %d, %d\n", a, b, c);
        return 0;
    }
    Anyway, I notice that at the end, you print a, b and c, rather than high, med and low. Also, I believe that there are 6 ways by which 3 numbers can be ordered, but you only cater to 4.
    Wow thanks...I couldn't have done it without the help from you guys


  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In other words, consider all these possibilities

    a<b<c
    a<c<b
    b<a<c
    b<c<a
    c<a<b
    c<b<a

    Your code doesn't account for all these.

    [edit]
    Replied too slow: good to see you got it.
    [/edit]
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16

    Post testing

    You are not checking all the possible conditions...
    • a<b<c
    • a<c<b
    • b<a<c
    • b<c<a
    • c<a<b
    • c<b<a

    & i'd suggest using nested ifs...
    [edit]
    I was even slower...
    [/edit]
    Last edited by Tanu; 01-29-2013 at 09:49 PM. Reason: late reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting many values from highest to lowest
    By Josh Mankey in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2012, 11:44 PM
  2. Highest and lowest value
    By manolo in forum C Programming
    Replies: 1
    Last Post: 01-23-2010, 10:57 AM
  3. HELP: Exclude the highest and lowest number...
    By Ocin101 in forum C Programming
    Replies: 12
    Last Post: 07-21-2009, 01:51 AM
  4. Sorting an int array highest to lowest
    By switchback in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 03:30 AM
  5. Trying to sort numbers from lowest to highest
    By jw232 in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2008, 04:03 PM