Sorting numbers in descending order

This is a discussion on Sorting numbers in descending order within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been working on this program and I don't get the right results for some odd reason. I want ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    Sorting numbers in descending order

    Hi, I've been working on this program and I don't get the right results for some odd reason. I want "all" of the following numbers to be in descending order.

    12180
    13240
    19800
    22458
    17000
    18125
    15623
    3200
    6500
    11970
    8900
    10000
    6200

    Here's the code, I do get the first three values in the list but none of the other numbers display. I want all the number to display in descending order. Where am I going wrong? Thank you for the help.

    Code:
    void Print_descending(const int list[], int length)
    {
               int index;
               int max;
               
               cout << "The numbers in descending order: ";
    
               max = list[0];
    
               for (index = 0; index < length; index++)
               {
                    while (list[index] > max)
                    max = list[index];
               }
               cout << max << endl;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    The first thing you do is separate sorting from printing

    sort_array( arr, len );
    print_array( arr, len );

    Then do a board search for sorting, it's a popular question.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    try instead of while (list[index] > max)

    if (list[index] > max)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting an Array of 5 Numbers in Order
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2009, 07:20 AM
  2. Sorting numbers in a file
    By pxleyes in forum C Programming
    Replies: 19
    Last Post: 04-15-2004, 06:17 AM
  3. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  4. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM
  5. display numbers in order
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2001, 09:16 PM

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