Thread: Trying to sort numbers from lowest to highest

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Trying to sort numbers from lowest to highest

    Well I'm trying to sort three integers from lowest to highest without using loops, nested if-statements, multiple statements in the if-statements, or else statements. Is this possible?

    This is what I've come up with so far:
    Code:
    //Trying to sort numbers from lowest to highest
    
    #include "C:\Visual C++ Projects\std_facilities_lib.h"
    
    void main()
    {
    	int i1; int i2; int i3;
    	int low; int med; int high;
    	cout << "Enter three integers separated by spaces:\n";
    	cin >> i1 >> i2 >> i3;
    	if (i1<i2)
    		if (i1<i3)
    			low=i1;
    	if (i2<i1)
    		if (i2<i3)
    			low=i2;
    	if (i3<i1)
    		if (i3<i2)
    			low=i3;
    
    
    	keep_window_open();
    }
    It only finds the lowest number. I don't know how to find the lower number of the next two.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    First, that won't work. As mentioned before, those are nested if statements, which aren't allowed. I would imagine this would take lots of if statements given those restrictions.

    I would approach this problem by assigning low to the first number. Then I'd compare that to the second number and update the low if necessary. Then do the same with the third number.

    You can follow the same steps for the high. For the medium, I guess you can do something similar to figure out which of the three numbers has not been used.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well I'm trying to sort three integers from lowest to highest without using loops, nested if-statements, multiple statements in the if-statements, or else statements. Is this possible?
    Yes.
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Somebody just asked this problem, although they were looking specifically for the middle value. I believe a board search is in order.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, you could sort them, that's an easy way to know which is the lowest, highest and which is in middle

    Code:
    if (i2 > i3)
        // swap i2 and i3
    if (i1 > i2)
        // swap i1 and i2
    if (i2 > i3)
        // swap i2 and i3
    /* A: i1 is lowest number, i2 is middle, i3 is highest */

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    foxman's example looks correct, though std::swap() would actually make it work
    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

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    14
    I am not sure about your question. did you try the conditional operator?
    something like:

    if (i1<i2)
    {
    low =(i1<i3 ? i1 : i3);
    med = (i2<i3 ? i2 : i3);
    high =(i2<i3 ? i3 : i2);
    }
    if (i2<i1)
    {
    low =(i2<i3 ? i2 : i3);
    med = (i1<i3 ? i1 : i3);
    high =(i1<i3 ? i3 : i1);
    }

    but the problem is it is multiple statements.
    Last edited by jojo13; 01-19-2008 at 01:35 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But really. Think how you would do this in the real world. If you were given 3 numbers and had to sort them and put them in their proper place, how would you do it?
    Code:
    void main()
    Read http://cpwiki.sourceforge.net/index.php/Void_main
    Last edited by Elysia; 01-19-2008 at 02:18 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    I'm going to assume that you either haven't learned about vectors yet, or that you are trying to figure this out without using vectors. Anyway, using the vector function sort() would be a better alternative in this case.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Eh, std::sort() would be best of all, but that's just cheating.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Anyway, using the vector function sort() would be a better alternative in this case.
    There is no such function for the standard vector container
    Besides, this appears to be either homework or a friendly challenge from some book/website, so resorting to a sort function (pun intended) from an existing library would be against the spirit of the problem (and possibly overkill for 3 items).
    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

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    Quote Originally Posted by laserlight View Post
    There is no such function for the standard vector container
    Yeah yeah, I meant algorithm. :P

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jw232 View Post
    This is what I've come up with so far:
    Code:
    //Trying to sort numbers from lowest to highest
    
    #include "C:\Visual C++ Projects\std_facilities_lib.h"
    
    void main()
    {
    	int i1; int i2; int i3;
    	int low; int med; int high;
    	cout << "Enter three integers separated by spaces:\n";
    	cin >> i1 >> i2 >> i3;
    	if (i1<i2)
    		if (i1<i3)
    			low=i1;
    	if (i2<i1)
    		if (i2<i3)
    			low=i2;
    	if (i3<i1)
    		if (i3<i2)
    			low=i3;
    
    
    	keep_window_open();
    }
    BTW, you do know that backslashes need to be escaped if you want literal backslashes to appear in your string. i.e. use "\\" instead of "\".

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Using absolute paths is not usually the best solution. It's better to use relative paths or <>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    Quote Originally Posted by cpjust View Post
    BTW, you do know that backslashes need to be escaped if you want literal backslashes to appear in your string. i.e. use "\\" instead of "\".
    Hmm it still worked anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. Displaying highest and lowest values
    By beastofhonor in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2006, 08:24 PM
  3. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  4. Help On A Quick Sort Program
    By nick4 in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2004, 10:51 AM
  5. interpolation sort
    By saigonara in forum C Programming
    Replies: 5
    Last Post: 07-14-2002, 06:58 PM