Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

POINTERS HOMEWORK In the card-shuffling and dealing program of Fig. 5.34, we int

ID: 3876873 • 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 20

Explanation / Answer

In this program, we have used the rand function to find the random index.

while traversing through the deck we have replaced the card with the card at the random index

C Code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define MAX_ROWS 4
#define MAX_COLUMNS 13

/*intializing the deck*/
void initialize_deck(int cards[MAX_ROWS][MAX_COLUMNS])
{
int i,j;
for(i =0; i < MAX_ROWS; i++)
{
for(j=0; j< MAX_COLUMNS; j++)
{
cards[i][j] = i * j + j + 1;
}
  
}
}

/*Reshufling the deck*/
void reshuffle_deck(int cards[MAX_ROWS][MAX_COLUMNS])
{
int i, j, rand_row, rand_col;
srand(time(0));
for(i =0; i < MAX_ROWS; i++)
{
for(j=0; j< MAX_COLUMNS; j++)
{
rand_row = rand() % MAX_ROWS;
rand_col = rand() % MAX_COLUMNS;
  
/*swapping the value with random index value*/
cards[rand_row][rand_col] = cards[rand_row][rand_col] + cards[i][j];
cards[i][j] = cards[rand_row][rand_col] - cards[i][j];
cards[rand_row][rand_col] = cards[rand_row][rand_col] - cards[i][j];
}
  
}
  
}

/*printing the deck*/
void print_deck(int cards[MAX_ROWS][MAX_COLUMNS])
{
int i,j;
for(i =0; i < MAX_ROWS; i++)
{
for(j=0; j< MAX_COLUMNS; j++)
{
printf("%3d ",cards[i][j]);
}
printf(" ");
}
}

int main() {
int cards[MAX_ROWS][MAX_COLUMNS];
initialize_deck(cards);
reshuffle_deck(cards);
printf("Shuffled deck ");
print_deck(cards);

}

-------------------------------------------------------------------------------------------------------------------------

output screenshot:

Shuffled deck
7 41 1 19 10 0 1 37 17 25 28 9 5
31 2 45 19 34 0 23 29 9 21 33 5 21
11 1 25 13 17 5 25 7 10 7 0 8 9
12 16 49 4 22 1 3 11 13 13 13 15 37

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote