a varitation of the problem is to pick cards from a shuffled
a varitation of the problem is to pick cards from a shuffled deck of 52 cards repeadtedly and fins out how many picks from are nedded before picking anther. write the program to simulate the number of picks needed to get four cards from each suit and display the four cards picked (it is possible card may be picked twice.
Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct card {
const char *face;
const char *suit;
};
typedef struct card Card;
void fillDeck( Card * const wDeck, const char * wFace[],
const char * wSuit[] );
void shuffle( Card * const wDeck );
void pickcards( const Card * const wDeck );
int main( void )
{
Card deck[ 52 ];
const char *face[] = { \"Ace\", \"Deuce\", \"Three\", \"Four\", \"Five\",
\"Six\", \"Seven\", \"Eight\", \"Nine\", \"Ten\",
\"Jack\", \"Queen\", \"King\"};
const char *suit[] = { \"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"};
srand( time( NULL ) );
fillDeck( deck, face, suit );
shuffle( deck );
pickcards( deck ); return 0;
}
void fillDeck( Card * const wDeck, const char * wFace[],
const char * wSuit[] )
{
int i;
for ( i = 0; i <= 51; i++ ) {
wDeck[ i ].face = wFace[ i % 13 ];
wDeck[ i ].suit = wSuit[ i / 13 ];
}
}
void shuffle( Card * const wDeck )
{
int i;
int j;
Card temp;
for ( i = 0; i <= 51; i++ ) {
j = rand() % 52;
temp = wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
void pickcards( const Card * const wDeck )
{
int i;
int H, D, S, C;
H=0; D=0; S=0; C=0;
for ( i = 0; i <= 51; i++ ) {
printf( \"%5s of %-8s%c\", wDeck[ i ].face, wDeck[ i ].suit,
\'\ \' );
if (strcmp(wDeck[i].suit, \"Hearts\")==0) H=1;
else if (strcmp(wDeck[i].suit, \"Diamonds\")==0) D=1;
else if (strcmp(wDeck[i].suit, \"Spades\")==0) S=1;
else if (strcmp(wDeck[i].suit, \"Clubs\")==0) C=1;
if (H+D+S+C ==4) break;
}
printf(\"No of cards picked is %d\",i);
}

