What wrong with this shuffle function?

This is a discussion on What wrong with this shuffle function? within the C Programming forums, part of the General Programming Boards category; I have a 2D array with 108 strings. How do I shuffle it correctly? I don't think I'm doing it ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    >char temp[13];
    char temp[208];

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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Describe in words what is the purpose of line 9.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 03: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, 03:59 PM
  5. a shuffle program
    By *Tim* in forum C Programming
    Replies: 7
    Last Post: 11-17-2002, 10:59 AM

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