Quote:
Originally posted by sbasak
this code has same logic as given by transcendental. But here I added a generic swap function :-) Note that I initialized the card array as string, S for Spade, H for Hearts, D Diamonds, C Clubs
// to shuffle pack of cards
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
// generic swap function
void swap(void **x,void **y)
{
void *temp;
temp = *x;
*x = *y;
*y = temp;
}
void main(void)
{
// cards array
char* d[] = {"1C","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC",
"1D","2D","3D","4D","5D","6D","7D","8D","9D","10D","JD","QD","KD",
"1H","2H","3H","4H","5H","6H","7H","8H","9H","10H","JH","QH","KH",
"1S","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS"};
// size
int s = 51;
// to shuffle
int i,x;
// to randomize seed
srand(time(NULL));
// do shuffling
for(i=s;i>=0;i--)
{
x=rand()*i/RAND_MAX;
swap((void**)&d[i],(void**)&d[x]);
}
// print shuffled cards
cout << "shuffled cards are" << endl;
for(i=0;i<=s;i++)
cout << d[i] << " ";
}
Just a few things.