Thread: What wrong with this shuffle function?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    9

    What wrong with this shuffle function?

    I have a 2D array with 108 strings. How do I shuffle it correctly?
    I don't think I'm doing it right

    Code:
    void Shuffle( char dest[208][13] )
    {
       // Initialize variables
       char temp[13];
       
       // Loop and shuffle(swap array values)
       for (int i=208; i>1; i-- )
        {
         int m = i + rand()%(208-i);
         strcpy(temp,dest[m]); // 
         strcpy(dest[m],dest[i]);
         strcpy(dest[i],temp);
          
         cout << destination[i] << " ";
        }

    The program force closes.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >char temp[13];
    char temp[208];

    The character array should be the same size as your strings can be.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Describe in words what is the purpose of line 9.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > The character array should be the same size as your strings can be.
    It is.
    The 208 is the major dimension, not the minor one (which is just 13, the same as the local).

    > for (int i=208; i>1; i-- )
    However, starting at 208 is an array overrun, since the max subscript is 207

    > cout << destination[i] << " ";
    OK, so where is the real code?
    1. You're using cout in a C program. If this is really C++ then there are many things you should be doing differently, like using std::vector in place of arrays and std::string in place of char arrays.
    2. There is no variable in scope called destination, so how did this even compile?
    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.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    Quote Originally Posted by vart View Post
    Describe in words what is the purpose of line 9.
    I;m not sure.
    And the destination in the cout is just a typo. Should be dest.
    I haven't learn about std::vector or anything like that yet.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by defjamvan View Post
    I;m not sure.
    If you do not know where you want to get it is not important where you go.

    If you do not know what your code should do - it is not important what it really does
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Sep 2013
    Location
    Somewhere on Earth
    Posts
    2
    I don't understand why line 9 is there. How about you just randomly generate two strings from the 207 strings and swap them? It's easier. (Maybe it takes longer time than the original method you were using, but then I don't really know what you were using, and you couldn't explain the purpose of line 9.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to shuffle the buttons in MFC
    By cool friend in forum Windows Programming
    Replies: 1
    Last Post: 12-30-2009, 04:57 PM
  2. Card Shuffle Help
    By killmequick in forum C Programming
    Replies: 26
    Last Post: 04-03-2008, 04:00 PM
  3. Array Shuffle
    By ypramesh in forum C Programming
    Replies: 2
    Last Post: 04-08-2006, 11:01 AM
  4. Deck Shuffle
    By pjharris in forum C++ Programming
    Replies: 51
    Last Post: 01-06-2006, 04:59 PM
  5. a shuffle program
    By *Tim* in forum C Programming
    Replies: 7
    Last Post: 11-17-2002, 11:59 AM