POINTERS HOMEWORK In the card-shuffling and dealing program of Fig. 5.34, we int
ID: 3876887 • Letter: P
Question
POINTERS HOMEWORK In the card-shuffling and dealing program of Fig. 5.34, we intentionally used an inefficient shuffling algonithm that introduced the possibility of indefinite postponement. In this problem, you will create a high-performance shuffling algorithm that a voids indefinite postponement. Modify Fig. 5.24 as follows. Initialize the deck array as shown in Fig. 5.35. Modify the shuffle function to loop row by row and column by column through the array touching every element once. Each element should be swapped with a randomly selected element of the array. Print the resulting array to determine if the deck is satisfactorily shuffled (as in Fig. 5.36, for example). You may want your program to call the shuffle function several times to ensure a satisfactory shuffle. Note that although the approach in this problem improves the shuffling algorithm, the dealing algorithm still requires searching the deck array for card 1, then card 2, then card 3, and so on. Worse yet, even after the dealing algorithm locates and deals the card, the algorithm continues searching through the remainder of the deck. Modify the program of Fig. 5.24 so that once a card is dealt, no further attempts are made to match that card number, and the program immediately proceeds with dealing the next card. Unshused deck anay 0 23 4 6 7 8 9 10 1 1 0i 23 4 5 6 789 10 11 12 13 1 14 15 16 17 18 19 20 21 22 23 24 25 26 2 27 2 29 3031 32 33 34 35 36 37 38 39 3 40 41 42 43 44 45 46 47 48 49 50 51 52 Fig. 5.35 Unshuffled deck array Sample shutfled deck anay 0 2 3 4 56 789 10 1 12 0 19 40 27 25 36 46 10 34 35 41 18 113 28 14 16 21 30 11 31 17 24 7 1 2 12 33 15 42 43 23 45 3 29 3247 26 3 S0 38 52 39 4 519 s 37 4922 20Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <set>
using namespace std;
void printDeck(int **deck);
void swap(int *a, int *b);
void shuffleDeck(int** deck, int row, int col);
int main() {
// deck with 4 rows and 13 columns
int **deck;
deck = new int*[4];
// initialize the deck
for(int i=0; i<4; ++i) {
deck[i] = new int[13];
}
int cardNo = 1;
//Initialize the deck of cards
for(int i=0; i<4; i++) {
for(int j=0;j<13; j++) {
*(*(deck + i) + j) = cardNo;
cardNo ++;
}
}
cout<<"Unshuffled Deck array : "<<endl;
printDeck(deck);
// Shuffle the cards
shuffleDeck(deck, 4, 13);
cout<<" Shuffled Deck array : "<<endl;
printDeck(deck);
return 0;
}
// Print the deck of cards
void printDeck(int **deck) {
for(int i=0; i<4; i++) {
for(int j=0;j<13; j++) {
cout << *(*(deck + i) + j) << " ";
}
cout << " " << endl;
}
}
// swap integers a with b passed by reference
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Shuffle the deck of cards
void shuffleDeck(int** deck, int row, int col) {
set<int> cardsShuffled;
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
// generate a random row number and column number
int randRow = rand() % row;
int randCol = rand() % col;
// Get the random card from the deck of cards
int randCard = *(*(deck + randRow) + randCol);
// check if the random card is already present in the set, then return 1 else 0
while(cardsShuffled.count(randCard) != 0) {
randRow = rand() % row;
randCol = rand() % col;
randCard = *(*(deck + randRow) + randCol);
}
// if the card is not present the deck then swap the card and insert into the deck
swap(*(*(deck + i) + j), *(*(deck + randRow) + randCol));
cardsShuffled.insert(*(*(deck + randRow) + randCol));
}
}
}
/*
Sample Output
Unshuffled Deck array :
1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50 51 52
Shuffled Deck array :
21 18 26 38 28 19 12 48 42 45 5 13 3
30 8 6 40 20 39 17 15 52 51 11 24 27
7 35 4 32 9 2 16 49 14 29 36 34 47
10 1 22 46 23 50 33 37 43 31 41 25 44
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.