Thread: Why does the output for this program come out "reverse"?

  1. #1
    Registered User
    Join Date
    Jan 2008

    Why does the output for this program come out "reverse"?

    Hi all,
    I am trying to get ExchangeSort to output numbers from an array to be in increasing order, yet the results come out in decreasing order, or as I put it, in reverse. Can anyone tell me how to get the results in increasing order?

    using namespace std;
    int main(void)
    	int array[5];		// An array of integers.
    	int length = 5;		// Length of the array.
    	int i, j;
    	int temp;
    	//Some input
    	for (i = 0; i < 5; i++)
    		cout << "Enter a number: ";
    		cin >> array[i];
    	for(i = 0; i < (length -1); i++)
    		for (j=(i + 1); j < length; j++)
    			if (array[i] < array[j])
    				temp = array[i];
    				array[i] = array[j];
    				array[j] = temp;
    	//Some output
    	for (i = 0; i < 5; i++)
    		cout << array[i] << endl;
    Example of input: (in order from first to last) 7, 3, 5, 6, 2

    The result ends up as 7,6,5,3,2 instead of what should be 2,3,5,6,7. Any help would be appreciated. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Umm . . . why don't you change this
    if (array[i] < array[j])
    to this?
    if (array[i] > array[j])
    If you can't figure out how to do that, perhaps you should read up on the basic algorithm for a bubble sort.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2008
    15 it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  2. Unusual program Output, Help
    By capvirgo in forum C Programming
    Replies: 8
    Last Post: 02-06-2008, 03:13 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  5. Program Output
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 10:32 PM