Problem with ascending order Program

This is a discussion on Problem with ascending order Program within the C Programming forums, part of the General Programming Boards category; Alright, I have written a program for class that allows the user to input three integers then sorts then and ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Problem with ascending order Program

    Alright, I have written a program for class that allows the user to input three integers then sorts then and prints them in ascending order.

    It works sometimes but sometimes it prints strange results. I'll post the code below and then beneath the code I'll give you a few test results. Any help would be greatly appreciated.

    Code:
    // This program takes integers from user input and
    // outputs them in ascending order.
    
    #include <stdio.h>
    
    main (void)
    {
    
            int a, b, c, min, mid, max;
    
            printf("Enter Number 1: ");
            scanf("%d", &a);
            printf("Enter Number 2: ");
            scanf("%d", &b);
            printf("Enter Number 3: ");
            scanf("%d", &c);
    
    
            if (a > b) {
                 if (a > c) {
                    max = a;
                      if (b > c){
                            mid = b;
                            min = c;
                      }
                      else
                            mid = c;
                            min = b;
                 }
                 else
                    max = c;
                    mid = a;
                    min = b;
    
            }
            else if (c > b) {
                    max = c;
                    mid = b;
                    min = a;
            }
            else if (b > c) {
                    max = b;
                      if (a > c) {
                            mid = a;
                            min = c;
                    }
                     else
                            mid = c;
                            min = a;
            }
    
            printf("%d \n%d \n%d \n", min, mid, max);
    
            return 0;
    }
    When I input 1, 2, 3... it prints out the correct order.

    When I input 3, 2, 1... it prints out 2, 3, 3.

    When I input -1, -2, -3 it prints out -2, -1, -1

    When I input -3, -2, -1 it prints out -3, -2, -1

    When I input -3, -2, -4 it prints out -3, -3, -2

    It seems that when a < b the results are wrong..

    Does anyone have any ideas? I'm sure its obvious to a fresh pair of eyes, I've just been looking at it for hours and my brain is fried

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the code looks pretty long and, although i havent taken a very good look at it, perhaps you could draw out on paper a method of finding the min, middle, and max of 3 numbers and discover a more efficient method. when doing so, think not in terms of programming, just simple english/math/logic, then convert your algorithm to code.

    an important thing to point out that i notice is your 'else' blocks. in each of your 3 'else' blocks you have more than one statement, however, only the first of the n statements belongs to the actual 'else' block. for example, if your 'if' clause gets executed, it will then execute the statement after the first statement after the 'else'.
    what im saying may not be clear.. the point is, just like your 'if' statements, you need to put {}s around your else block when it has more than one statement to be executed.

    for example:
    Code:
    if(...)
       {
           ;//...
       }
    else
       mid = c;
       min = a;
    if the 'if' condition is false, it will of course go to the 'else' clause. it will then execute the statement 'mid = c';. then it will execute the 'min = a'; statement, which seems fine. however, if the 'if' condition is true, it will, again, of course, execute whatever its supposed to in the if block. after those statements, it will also execute 'min = a';. so in both cases 'min = a'; is executed, so you should probably put {} around the statements in your else clauses' if you want them to be executed only 'else'. im sure this will be part of your problem.

    hope it makes sense.
    Last edited by nadroj; 09-20-2007 at 08:51 PM.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    You could also try using a sorting function like bubble or selection sort. They are bit more complex to code but they do also reduce the code lines you have to write as they are small compact functions. Oh and there is also quicksort
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    try using && "and", or || "or" operators...
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Thumbs up

    using the && method worked, thanks for your suggestion, greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 09:45 AM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  3. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM

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