Multi dimensional array

This is a discussion on Multi dimensional array within the C++ Programming forums, part of the General Programming Boards category; In the book im studing it shows you how to bubble sort a single subscripted array. I see nothing on ...

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Multi dimensional array

    In the book im studing it shows you how to bubble sort a single subscripted array. I see nothing on doing that with a double subscripted array. So I have just been messing with it to see and I cant get it sort the array. Here's the code.
    Code:
    void bubbleSort(int c[][5])
    {
    	int hold;
    	for ( int i = 0; i < 3; i++ )
    
    		for ( int j = 0; j < 5 - 1; j++ )
    
    			if( c[i][j] > c[i][ j + 1 ] ) {
    				hold = c[i][j];
    				c[i][j] = c[i][ j + 1 ];
    				c[i][ j + 1 ] = hold;
    			}
    			for( int k = 0; k < 3; k++ ) {
    
    				for( int l = 0; l < 5; l++ )
    
    					cout << c[k][l] << ' ' ;
    			}
    }
    Im not at the level of vectors and such. I run this on the array and it moves some of them but it doesnt put them in the proper order.
    big146

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    How you sort a 2D array depends on what you expect to see when you've finished

    4 5 6
    1 2 3
    7 8 9

    Sorted by [x][0] gets you this for example

    1 2 3
    4 5 6
    7 8 9
    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 big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Hi

    Basicly I just want to sort a 3 x 5 array in ascending order. I have used the algorithym posted for single subscripted arrays and it works fine.But I cant get it to work for double subscripted arrays.
    the code posted above is for the array im using.
    big146

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way. Use a double for-loop like:
    Code:
       for (int i=0; i<3*5-1; i++)
       {
          for (int j=i; j<3*5; j++)
          {
          }
       }
    Then you can calculate row and column of your array by using division(/) and modulus(%).

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The problem that Salem is getting at is that 'ascending order' is not well defined for a multi-dimensional array. You could set them up as in Salem's example or arrange them column-wise, or a host of other (probably somewhat absurd) variations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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