Thread: Sorting an array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I see what you did there. Might work if I inputted directly to the array and then just moved to the left until it hit a number lower than itself.

    edit: It works now! And the code got a little shorter:
    Code:
    #include <iostream.h>
    #define ANTAL 9
    
    int main()
    {
    	int tal[ANTAL + 1], i;
    
    	for (i = 0; i <= ANTAL; i++) {
    		cout << "Skriv ett tal: ";
    		cin >> tal[i];
    
    		for (int z = i; z; z--)
    			if (tal[z - 1] > tal[z]) {
    				int temp = tal[z - 1];
    				tal [z - 1] = tal[z];
    				tal[z] = temp;
    			}
    			else
    				break;
    	}
    
    	for (i = 0; i <= ANTAL; i++)
    		cout << tal[i] << " ";
    
    	return 0;
    }
    Last edited by OnionKnight; 04-24-2005 at 03:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM