Modify the card shuffling and dealing program below so the shuffling and dealing
ID: 3888198 • Letter: M
Question
Modify the card shuffling and dealing program below so the shuffling and dealing operations are performed by the same function shuffleAndDeal. The function should contain one nested lopping structure that’s similar to function shuffle.
Code in C language.
#include
#include
#include
#define SUITS 4
#define FACES 13
#define CARDS 52
//prototypes
void shuffle(unsigned int wDeck[][FACES]); //shufling modifies wDeck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]); //dealing doesn't modify arrays
int main()
{
//initialize suit array
const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
//initialize face array
const char *face[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
//initialize deck array
unsigned int deck[SUITS][FACES] = {0};
srand(time(NULL)); //seed random-number generator
shuffle(deck);
deal(deck, face, suit);
}
void shuffle(unsigned int wDeck[][FACES])
{
size_t row; //row #
size_t column; //column #
size_t card; //counter
//for each of the cards, choose slot of deck randomly
for (card = 1; card <= CARDS; ++card)
{
//choose new random location until unoccupied slot found
do
{
row = rand() % SUITS;
column = rand() % FACES;
} while(wDeck[row][column] != 0);
//place card # in chosen slot of deck
wDeck[row][column] = card;
}
}
//deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[])
{
size_t card; //card counter
size_t row; //row counter
size_t column; //column counter
//deal each of the cards
for(card = 1; card <= CARDS; ++card)
{
for(row = 0; row < SUITS; ++row)
{
for(column = 0; column < FACES; ++column)
{
//if slot contains current card, display card
if (wDeck[row][column] == card)
{
printf("%5s of %-8s ", wFace[column], wSuit[row], card % 2 == 0 ? ' ' : ' ');
}
}
}
}
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
//Prototype of the function
//Dealing doesn't modify arrays because the parameters of type pointer
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]);
//Main method definition
int main()
{
//Initialize suit array
const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
//Initialize face array
const char *face[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
//Initialize deck array
unsigned int deck[SUITS][FACES] = {0};
//Seed random-number generator
srand(time(NULL));
//Calls the method to shuffle and deal operation
deal(deck, face, suit);
}
//Function to Shuffle and Deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[])
{
//Counter for Card
size_t card;
//Counter for Row
size_t row;
//Counter for column
size_t column;
//Shuffle operation
//For each of the cards, choose slot of deck randomly
for (card = 1; card <= CARDS; ++card)
{
//Choose new random location until unoccupied slot found
do
{
//Generates a random number for row
row = rand() % SUITS;
//Generates a random number for column
column = rand() % FACES;
} while(wDeck[row][column] != 0); //End of do-while loop
//Place card # in chosen slot of deck
wDeck[row][column] = card;
}//End of for loop
//Deal operation
//Deal each of the cards
for(card = 1; card <= CARDS; ++card)
{
//Loops 4 times for suits
for(row = 0; row < SUITS; ++row)
{
//Loops 12 times for faces
for(column = 0; column < FACES; ++column)
{
//If slot contains current card then display the card
if (wDeck[row][column] == card)
{
printf("%5s of %-8s ", wFace[column], wSuit[row], card % 2 == 0 ? ' ' : ' ');
}//End of if condition
}//End of inner loop for faces
}//End of inner loop for suits
}//End of outer loop for cards
}//End of method
Sample Run:
Five of Diamonds
Three of Diamonds
King of Spades
Three of Hearts
Four of Clubs
Five of Spades
Seven of Hearts
King of Hearts
Five of Hearts
King of Clubs
King of Diamonds
Deuce of Hearts
Queen of Spades
Nine of Diamonds
Six of Clubs
Nine of Hearts
Deuce of Diamonds
Queen of Hearts
Ten of Clubs
Four of Diamonds
Ace of Diamonds
Nine of Spades
Nine of Clubs
Jack of Clubs
Jack of Spades
Seven of Diamonds
Six of Diamonds
Ten of Diamonds
Three of Spades
Seven of Spades
Eight of Clubs
Jack of Diamonds
Eight of Diamonds
Three of Clubs
Deuce of Spades
Eight of Spades
Four of Hearts
Six of Spades
Ten of Hearts
Six of Hearts
Ace of Spades
Ten of Spades
Seven of Clubs
Ace of Hearts
Queen of Diamonds
Queen of Clubs
Deuce of Clubs
Four of Spades
Ace of Clubs
Jack of Hearts
Eight of Hearts
Five of Clubs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.