Thread: shuffle 2-dimensional array

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

    shuffle 2-dimensional array

    Hi,

    I found this code for shuffle one-dimensional int array somewhere:

    Code:
    void shuffle(int *array, size_t n)
    {
        if (n > 1) {
            size_t i;
    	for (i = 0; i < n - 1; i++) {
    	  size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
    	  int t = array[j];
    	  array[j] = array[i];
    	  array[i] = t;
    	}
        }
    }
    How can I make change to this function to shuffle 2-dimensional array. This is the declaration of the 2-dimensional array I need to shuffle.

    Code:
    char list[2000][20];
    This array is an array of string (20 char max). I need to shuffle the outer array not the inner one.

    Any help would be really appreciated,
    Pat

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So have a temporary 20 char string and use strcpy to the swap.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    3rd forum I've seen this on - spamalot?
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What do you mean when you say need to shuffle the outer array not the inner one.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    What do you mean when you say need to shuffle the outer array not the inner one.
    From this:
    Code:
    char arr[3][10] = { "Hello", "New", "World" };
    
    shuffle(arr);
    we want something like "New", "World", "Hello" in the array, not "eNl", "Hwldo" ,"Wrdll" [I'm not sure that's actually not lost or gained some letters, but I think it gets the message across].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Well thanks for the great explanation matsp, I thought it is the exact opposite where the columns of each row are shuffled around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  2. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  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. Replies: 5
    Last Post: 11-20-2001, 12:48 PM