Question
(Write a program, and show the output too) C programming Revisit the card shuffling program in Pointers - Part A. #include #include #include int main
(Write a program, and show the output too) C programming
Revisit the card shuffling program in Pointers - Part A.
#include
int main ( ) {
// card is an array with 24 cells, each cell is of type char pointer // The strings are constant, the address of the first character // is constant and can't be changed.
// we assign that address to the cells in the array // This is similar to char *p = "Sample String" ;
char *card[24] = { "spade-one", "spade-two", "spade-three", "spade-four", "spade-five", "spade-six", "heart-one", "heart-two", "heart-three", "heart-four", "heart-five", "heart-six", "diamond-one", "diamond-second", "diamond-three", "diamond-four", "diamond-five", "diamond-six", "club-one", "club-two", "club-three", "club-four", "club-five", "club-six"
} ; int i ;
// Write A shuffle program to randomize the deck of cards // so each player will get different cards.
// Question1: TRACE THIS CODE FOR AT LEAST FIVE ITERATIONS // WHAT AM I DOING ? // WHAT AM I SWAPPING , VALUES, ADDRESSES? // GIVE AS MUCH AS DETAILS AS POSSIBLE srand( time ( NULL ) ); for ( i = 0 ; i < 24 ; i++ ) { char *temp ; int cardNo = rand( ) % ( 24 - i ) ; temp = card [ cardNo ] ; card [ cardNo ] = card [ 24 - i - 1 ] ; card [ 24 - i - 1 ] = temp ; }
printf ( " Printing the cards of player 1 " ); for ( i = 0 ; i < 24 ; i+=4 ) { printf ( " %s ", card [ i ] ); // add code to check if player 1 has three6s }
printf ( " Printing the cards of player 2 " ); for ( i = 1 ; i < 24 ; i+=4 ) { printf ( " %s ", card [ i ] ); // add code to check if player 2 has three 6s } printf ( " Printing the cards of player 3 " ); for ( i = 2 ; i < 24 ; i+=4 ) { printf ( " %s ", card [ i ] ); // add code to check if player 3 has three 6s } printf ( " Printing the cards of player 4 " ); for ( i = 3 ; i < 24 ; i+=4 ) { printf ( " %s ", card [ i ] ); // add code to check if player 4 has three 6s } // repeat the above code if any player has three 1s
}
Compile and run the above. Try to understand how I distributed the cards to the four players. As you can see, we are printing the cards of each player. if you want to search for a player who has a six, you could do if ( strstr ( card [ i ] , "six" ) != NULL ) statement, right ? Here, strstr returns NOT NULL if "six" is a substring of card [ i ] . Using the above fact, add code to find the winner. The winner is the player who has at least three 6s. Print all cards of the winner. If you can't find any player with three 6s, then search for a player who has least three 1s . Print all cards of the winner if you can't any player, then don't do anything, exit the program stating no winner found. You may have to run your program several times before you find a winner because the probability of finding a winner is very low. So Be Patient.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started