Arranging numbers in ascending order

This is a discussion on Arranging numbers in ascending order within the C++ Programming forums, part of the General Programming Boards category; How would I use C++ to arrange a set of 5 inputted values in ascending order and then cout the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Arranging numbers in ascending order

    How would I use C++ to arrange a set of 5 inputted values in ascending order and then cout the order? So far I have an idea of using an array, order[5], and then assigning the lowest number to order[0], second lowest number to order[1], etc. Then I would cout order[0], order[1], etc. I am having trouble on using C++ to find the second lowest number, the middle value, and the second highest number. Any help would be grateful.

    Is there a faster way to do this besides with arrays?

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    yes

    bubble sort maybe. here is what to do, its the basic idea:

    //count is how many values were entered
    int doagain=1;
    do
    {
    doagain=0;
    for(int i;i<=count;i++)
    {
    if(order(i)>order(i+1))
    {
    int temp=order(i);
    order(i)=order(i+1);
    order(i+1)=temp;
    doagain=1
    }
    }
    }while (doagain=1);

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    5
    I didn't learn bubble sort yet. I'm a beginner in C++. I know basics and for and while loops and arrays. Basically, I learned all the stuff up to Lesson 9 of the cprogramming.com tutorials.

  4. #4
    Unregistered
    Guest
    What you want is a sorting method, and since bubble sort is the simplest of them that would be the place to start. If you don't know how it works then break it down line by line to figure it out. If you haven't learned sorting yet, now is as good a time to start because it's one of the basic things that you learn when learning a language.

  5. #5
    Unregistered
    Guest
    Okay I figured it out the long way without sorts:

    Code:
    int avg, a, b, c, d, e, f=0, v, w, x, y, z;
    
    cout<<"Input 5 numbers: ";
    cin>>a>>b>>c>>d>>e;
    
    v=a;
    w=b;
    x=c;
    y=d;
    z=e;
    
    cout<<"Here are the numbers in ascending order: "<<endl;
    
    while (f<5)
    {
         v--;
         w--;
         x--;
         y--;
         z--;
         if (v==0)
         {
              cout<<a<<endl;
              f++;
         }
         if (w==0)
         {
              cout<<b<<endl;
              f++;
         }
         if (x==0)
         {
              cout<<c<<endl;
              f++;
         }
         if (y==0)
         {
              cout<<d<<endl;
              f++;
         }
         if (z==0) 
         {
              cout<<e<<endl;
              f++;
         }
    }

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    5
    Oops I forgot to login. Oh well, that works right?

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    Selection Sort

    int help,j,k,num[5];

    for(j=0;j<4;j++)
    for(k=4;k>j;k--)
    if(num[j]>num[k])
    {
    help=num[j];
    num[j]=num[k];
    num[k]=help;
    }

    /* Selection sort is faster than bubble sort. Only quick sort is faster. */

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Sorting

    You are doing nothing but sorting. So, its best you pick up some sorting algorithms like bubblesort, insertion or selection.

    Or advanced ones like Binary tree an Heap Sort.

    I appreciate the idea of using while loop to do the soring (for only the idea, not for practical application.)

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. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM
  5. display numbers in order
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2001, 10:16 PM

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