Thread: 3 integers in ascending order-help

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    3 integers in ascending order-help

    For the life of me I can not figure out how to efficiently code placing three integers in ascending order! I can do it with a bazillion lines of code but I'm certain there is a shorter way.

    int num1;
    int num2;
    int num3;

    int min;
    int mid;
    int max;

    (I'm too embarrassed to write those bazillion lines of code!)

    any help would be greatly appreciated.

    Alli

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    what do you mean?

    something like this will work

    int num1,num2,num3;

    or

    int min,mid,max;
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    whoops! I meant to say that the 3 integers are read in from a file ... then need to be sorted into ascending order then displayed on the screen. I just don't know how to write the tests using say five lines of code.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well this is the algorithm - all you need to do is arrange the input, and figure out how to swap two variables

    Code:
        if ( a > b ) swap(a,b);
        if ( b > c ) swap(b,c);   // c is now the largest
        if ( a > b ) swap(a,b);   // a and b are also in order
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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