#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0

    writing from one array to another


    Hi,

    I have an array of strings- "AC", "2C" etc.

    To get them in a random order, I am producing a random number 1-52 and then writing it to a new array.

    Is this the correct code to do this?-

    <code>aShufflePack[cardIndex][MAX_LENGTH]=aDeckCards[iShufflePack][MAX_LENGTH];</code>

    cardIndex is the for loop that sequentially accesses each element of the array, and iShufflePack is the random number to select a card from the original initialised array.

    aDeckCards is the original array, and aShufflePack is the array to write to. At the moment I just get junk in each element.

    Thank you
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Use the square brackets for code tags.

    If you were copying integers from one array of integers to another, you would use this:

    Code:
    ShuffleArray[index1] = OriginalArray[index2];
    But, for strings, you cannot use the = operator. You need to use the strcpy() function. Assuming that both arrays are strings of length 2 (since you must be sure that the destination string is large enough to hold the source string), you could do this:

    Code:
    strcpy(ShuffleArray[index1],OriginalArray[index2]);
    If you reference ShuffleArray[index1][x] instead of just ShuffleArray[index1], you are referencing a particular character within the string, not the pointer to the string itself.

    I hope this clears it up for you.

    Btw, a good shuffling routine is to use the original array by itself, and swap two elements. Do this 52 times for your entire deck of cards, using the loop counter 0..51 for one index, and a random number for the other index, which ensures that at least every index was swapped once.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0
    Thank you for your help!

    It has sorted my problem out.

IMN logo majestic logo threadwatch logo seochat tools logo