Reference no: EM132211706
Question: (Write a program, and show the output too) C programming
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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 ( "\n Printing the cards of player 1 \n" );
for ( i = 0 ; i < 24 ; i+=4 )
{
printf ( " %s \n", card [ i ] );
// add code to check if player 1 has three6s
}
printf ( "\n Printing the cards of player 2 \n" );
for ( i = 1 ; i < 24 ; i+=4 )
{
printf ( " %s \n", card [ i ] );
// add code to check if player 2 has three 6s
}
printf ( "\n Printing the cards of player 3 \n" );
for ( i = 2 ; i < 24 ; i+=4 )
{
printf ( " %s \n", card [ i ] );
// add code to check if player 3 has three 6s
}
printf ( "\n Printing the cards of player 4 \n" );
for ( i = 3 ; i < 24 ; i+=4 )
{
printf ( " %s \n", 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 find 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.