Reputation: 35
Can someone show me how to randomize an array of structures. I've search online and seen many tutorial on how to randomsize array. but i dont think they work in my scenario...
I kept getting error .exe stopped working .....
i assume the problem comes from
card[i] = i + 1;
card[i] = card[j];
card[j] = temp;
Can someone please help me with my code, i am trying to randomise/shuffle the array card[]. for example:
card[0] is Ace heart red card[1] is Two HEart red ... and so on i want them to be card[0] is some other card now.
struct Cards
{
int suits;
int values;
int colour;
};
struct Cards card[52];
void printcard();
void filldeck(); /* Populates a deck of cards */
void printdeck();
void shuffle();
int main()
{
/* code */
printcard();
filldeck();
printdeck();
shuffle();
printdeck();
return 0;
}
void shuffle()
{
printf("%s\n\n", "---------------------- Shuffling deck ----------------------");
int i=0;
int j=0;
int temp=0;
int card[52];
srand(time(NULL));
for (i=0;i<52;i++)
{
j = (rand()%51) + 1;
temp = card[i];
card[i] = card[j];
card[j] = temp;
}
return;
}
Upvotes: 2
Views: 1704
Reputation: 5011
You have the statements :
int i,j,temp;
int card[i];
but you have not initialized i
. Therefore, you do not know how many elements array card
will have and the behaviour of your program will be undefined, probably leading to segmentation fault. If you want your array to have 52
elements, initialize i to :
int i = 52;
and then declare :
int card[i];
But actually, the array you want to shuffle is the global array struct Cards card[52]
, and not the array cards
defined inside shuffle
(see this link on variables' scope). So try removing the following lines of code :
int card[i];
for (i=0;i<52;i++)
{
card[i] = i + 1;
}
and see if the results are what you expected.
Upvotes: 2
Reputation: 86651
There are many problems with this code.
As Marievi says, this line
int card[i];
should be
int card[52];
because i is uninitialised at that point (you would have caught the error if you had compiled with lots of warnings enabled. If you are using gcc, compile with -Wall
.
This line
j = (rand()%51) + 1;
actually gives you a pseudo random number between 1 and 51 inclusive, not 0 and 51 inclusive, which I think is what you want because it should be an index into the array.
Most importantly, the array you are shuffling is not the array with the cards in it. The line that should be int card[52];
defines a new local array that exists only until the function returns and is then thrown away. It "shadows" your global card array for the duration of the function. If you delete it and get rid of the
for (i=0;i<52;i++)
{
card[i] = i + 1;
}
you'll find your global card array is shuffled.
One more thing: you don't need to putthe colour of your cards in the Card
struct. The colour is encoded by the suit. Hearts and diamonds are red; clubs and spades are black.
Upvotes: 1
Reputation: 151
In addition to the answer already given, in order to randomize pack of cards, please observe following: In function 'shuffle', you have created a local array 'card'; which eclipses global array 'card' of type 'struct Cards'. What you need to do is use global 'card' in your shuffle function instead of local 'card'. Local int array 'card' has life and visibility only inside shuffle().
HTH, Swanand
Upvotes: 0