Thread: Sorting three integers from the user in ascending order help

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    13

    Unhappy Sorting three integers from the user in ascending order help

    Hello,
    I'm trying to get a user to input 3 integers (including negatives) and then sort those three into ascending order. When i type in 4 -2 9 which is the example, i kept receiving -2 4 9 but no other examples would work. What's going on in my code? I tried something different for the second number being the largest. I think negative numbers are not being sorted correctly?

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #define MAX 10
    
    int main()
    {
    
        char a[MAX];
        char b[MAX];
        char c[MAX];
        int number1, number2, number3;
    
        printf("Enter 3 integers separated by a space: ");
        fflush(stdout);
        scanf("%s %s %s", a, b, c);
    
        number1=atoi(a);
        number2=atoi(b);
        number3=atoi(c);
    
        //Cases where first number is the biggest
        if (number1 >> number2 >> number3)
        {
            printf("%d %d %d\n", number1, number2, number3);
        }
    
        if (number1 >> number2 >> number3)
        {
            printf("%d %d %d\n", number1, number2, number3);
        }
            if (number1 >> number3 >> number2)
        {
            printf("%d %d %d\n", number1, number3, number2);
        }
    
    
        //Cases where second number is the biggest
            if ((number2 >> number1) & (number2 >> number3) & (number1 >> number3))
            {
                printf("%d %d %d\n", number2, number1, number3);
            }
    
           if ((number2 >> number3) & (number2 >> number1) & (number3 >> number1))
                {
                    printf("%d %d %d\n", number2, number3, number1);
                }
    
        //Cases where third number is the biggest
        if (number3 >> number1 >> number2)
                    {
                        printf("%d %d %d\n", number3, number1, number2);
                    }
                if (number3 >> number2 >> number1)
                    {
                           printf("%d %d %d\n", number3, number2, number1);
                    }
    
           return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,559
    >> does not do what you think it does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    13
    What should i be using instead? I figured it out, i needed >= instead of >>!
    Last edited by Passwaters; 1 Week Ago at 03:27 PM. Reason: Answered

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Learn your operators. Also, even if you were using the right ones, multiple comparisons don't work like that in C. You need to compare two operands at a time. In your case a set if min/max functions would probably be best.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    13

    Utilized two >= statements with an AND

    Code:
    #include <stdio.h>#include<stdlib.h>
    #define MAX 10
    
    
    int main()
    {
        char a[MAX], b[MAX], c[MAX];
        int number1, number2, number3;
        printf("Enter 3 integers separated by a space: ");
        fflush(stdout);
        scanf("%s %s %s", a, b, c);
    
    
        number1=atoi(a);
        number2=atoi(b);
        number3=atoi(c);
    
    
        //Cases where first number is the biggest
        if ((number1 >= number2) & (number2 >= number3))
        	{
        		printf("%d %d %d\n", number3, number2, number1);
        	}
    
    
        if ((number1 >= number3) & (number3 >= number2))
        	{
        		printf("%d %d %d\n", number2, number3, number1);
        	}
    
    
        //Cases where second number is the biggest
        if ((number2 >= number1) & (number1 >= number3))
        	{
            	printf("%d %d %d\n", number3, number1, number2);
            }
    
    
       if ((number2 >= number3) & (number3 >= number1))
            {
                printf("%d %d %d\n", number1, number3, number2);
            }
    
    
        //Cases where third number is the biggest
       if ((number3 >= number1) & (number1 >= number2))
            {
            	printf("%d %d %d\n", number2, number1, number3);
            }
       if ((number3 >= number2) & (number2 >= number1))
            {
            	printf("%d %d %d\n", number1, number2, number3);
            }
    
    
    return 0;
    }

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    The & operator does a bitwise AND. Check out logic operators.

  7. #7
    Registered User
    Join Date
    Sep 2018
    Posts
    13

    > vs >>

    Quote Originally Posted by Sir Galahad View Post
    The & operator does a bitwise AND. Check out logic operators.
    Noted and fixed, thanks for the response!

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    542
    In addition to what others have advised, you are not using scanf() as it was designed.

    scanf() will convert text data to an integer without having to use atoi().

    Instead of:

    Code:
    int main()
    
    {
    
        char a[MAX], b[MAX], c[MAX];
        int number1, number2, number3;
        printf("Enter 3 integers separated by a space: ");
        fflush(stdout);
    
        scanf("%s %s %s", a, b, c);
    
     
     
        number1=atoi(a);
    
        number2=atoi(b);
    
        number3=atoi(c);
    
            ...
    }
    try this:

    Code:
    int main()
    {
    
       int number1 = 0;
       int number2 = 0;
       int number3 = 0;
    
        printf("Enter 3 integers separated by a space: ");
        fflush(stdout);
        scanf("%d %d %d", &number1, &number2, &number3);
        ...
    }

    Of course, you should add error checking to inure you hae received valid data from scanf(), no matter which method you use.

    Also, you should initialize all your local variables!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-27-2012, 05:32 AM
  2. Sorting dates into ascending order
    By DarkEmpire in forum C Programming
    Replies: 2
    Last Post: 03-06-2012, 11:01 AM
  3. 3 integers in ascending order
    By hobilla in forum C Programming
    Replies: 7
    Last Post: 02-14-2009, 01:01 PM
  4. sorting arrays (ascending order)
    By utoots in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:57 AM
  5. 3 integers in ascending order-help
    By Allison23NY in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2001, 02:24 AM

Tags for this Thread