Thread: 3 integers in ascending order

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Unhappy 3 integers in ascending order

    hi

    this is my 1st post here, hope i'll get know learn many things.

    I am just starting an introductory course on C programming in ANSI in my university.

    the other day my sir gave me an assignment telling us to prompt the user for 3 integers and the output should give them in ascending order.

    I tried my best....and this is how it looks.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int a,b,c;
    
    void main (){
    
    	printf("Please enter 3 integers\n");
    	scanf("%d %d %d",  &a,&b,&c);
    	printf("The integers from smallest to largest are:\n");
    
    	if (a>b) {
    		if (b<c){
    		printf("%d %d %d", a,b,c);
    		}
    
    		else if (a<c){
    		printf("%d %d %d", a,c,b);
    		}
    		else {
    		printf("%d %d %d", c,a,b);
    		}
    
    	else{
    		if (b>c){
    		printf("%d %d %d", c,b,a);
    		}
    		else if (b<c){
    		printf("%d %d %d", b,c,a);
    		}
    		else {
    		printf("%d %d %d", b,a,c);
    		}
    	   }
           }
    }
    can some one please find me the flaw??? it doesnt run

    Hobilla.. Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your first if statement says "if (a>b)" but in each of the statements where that's true, you print a before b.

    Also, there's no such thing as "void main" and there's probably no good reason for the variables a, b, and c to live "outside" your program.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    ok i changed the a>b to a<b...then its ok right?

    what do i do with void main?

    by outside, what do you mean?

    should i place the integers after the main function line??

    how do i get it to run??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by hobilla
    what do i do with void main?
    Change it to int main. You should also return 0 at the end of the main function (though depending on your exact compiler settings, that may be optional for the main function).

    Quote Originally Posted by hobilla
    by outside, what do you mean?

    should i place the integers after the main function line??
    Yes, declaring those variables at the start of the main function would be appropriate.

    Quote Originally Posted by hobilla
    how do i get it to run??
    To be more precise: are you trying to get it to compile and run, or does it already compile but gives incorrect output? In both cases you should tell us how does it not work (though you have already been told a possible solution).
    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

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You haven't closed off your first if loop properly, you have it closed off after the second else statement. Which not only makes two else statements inside that if loop (think that creates a compiler error?), but it will not print anything if a is less than b.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Hi hobilla,

    your code is broken by design. No human would try to sort a set of three elements in this way, it's too complicated. Consider deleting all your code and re-start from scratch. Here are a few hints:

    1)
    You only need three comparisons to compare every element of the set with every other element (due to transitivity of "<="), e.g. "a<=b", "b<=c" and "c<=a" will do the trick.

    2)
    Without thinking about the actual implementation in C, consider the following situation: you have three balls, each of which has a different size. How would you sort them in ascending order? Hint: start with the two outmost balls.


    And more generally, here are some more tips:

    1) Don't nest if-statements. You're trying to put too much information in one sentence. Keep it simple.
    2) Use only one printf(). That's what you'd do in real life, too.


    Greets,
    Philip

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    thank u guys
    @laserlight: it duznt compile, has errors...And also, can u tel me the structure of the if else thingy...im really confused with them....nested if else?? else if ladder?? all these...:S

    @snafuist: thanks for your info. I already thought of it like that. But sorry, im relly weak in algorithms. I just cannot even figure out the 2 balls problem u wrote.
    can u please guide me with this..??

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by hobilla
    it duznt compile, has errors.
    You should post the updated code and the exact error messages.

    Quote Originally Posted by hobilla
    can u please guide me with this..?
    Think about swapping balls if they are not in the correct order.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to print 3 numbers in ascending order
    By jadedreality in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 07:32 PM
  2. Problem with ascending order Program
    By cashmerelc in forum C Programming
    Replies: 4
    Last Post: 09-21-2007, 05:36 PM
  3. Arranging numbers in ascending order
    By Aero in forum C++ Programming
    Replies: 7
    Last Post: 12-22-2001, 07:52 AM
  4. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 07:14 PM
  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