POINTERS HOMEWORK In the card-shuffling and dealing program of Fig. 5.34, we int
ID: 3876444 • 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 #include #include 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; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.