3 integers in ascending order

This is a discussion on 3 integers in ascending order within the C Programming forums, part of the General Programming Boards category; hi this is my 1st post here, hope i'll get know learn many things. I am just starting an introductory ...

  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,185
    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
    21,403
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,403
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06: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, 06:52 AM
  4. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 06:14 PM
  5. 3 integers in ascending order-help
    By Allison23NY in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2001, 01:24 AM

Tags for this Thread


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