Give me an algorithm and C code to shuffle a deck of cards, given that the cards are stored in an array of ints. Try to come up with a solution that does not require any extra space.
A sample implementation to shuffle a deck of cards might look like this:
void shuffle( int * cards)
{
int curr, victim, size, temp;
curr = 0;
size = 52;
// assuming it's a regular 52 card deck
while(curr < size)
{
victim = rand() % size;
cout << rand() << "\n";
temp = cards[curr];
cards[curr] = cards[victim];
cards[victim] = temp;
curr++;
}
}
void shuffle( int * cards)
{
int curr, victim, size, temp;
curr = 0;
size = 52;
// assuming it's a regular 52 card deck
while(curr < size)
{
victim = rand() % size;
cout << rand() << "\n";
temp = cards[curr];
cards[curr] = cards[victim];
cards[victim] = temp;
curr++;
}
}
Comments
Post a Comment
https://gengwg.blogspot.com/