September 12th, 2003, 06:49 AM
writing from one array to another
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?-
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.
September 12th, 2003, 07:14 AM
Use the square brackets for code tags.
If you were copying integers from one array of integers to another, you would use this:
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:
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.
September 13th, 2003, 06:28 AM
Thank you for your help!
It has sorted my problem out.