1 / 8

Tutorial 10

Tutorial 10. Question 2a , 2b , 2c by Kapilesh Ramesh. Question 2 .

skah
Télécharger la présentation

Tutorial 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 10 Question 2a , 2b , 2c by Kapilesh Ramesh

  2. Question 2 2) A deck of cards is commonly shuffled using the riffle shuffle technique in which half of the deck is held in each hand and the cards are released one by one in a left-right fashion such that they interleave into a new deck. A riffle shuffle is considered a perfect shuffle if the cards are perfectly interleaved in an orderly fashion. There are two types of pˆerfect riffle shuffles • In-shuffle: the original top card ends up second from top in the shuffled deck. • ˆOut-shuffle: the original top and bottom cards are preserved in the shuffled deck. Your task is to implement a deck of 52 cards and perform a number of (in or out) shuffles. Use the following as a guide in your implementation

  3. Question 2 Part a 2a) Each card has a rank and suit. The suit is represented as one of four characters ’C’, ’D’, ’H’ or ’S’ (for clubs, diamonds, hearts and spades). The rank can be "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q" or "K". Define a C structure Struct Card to represent a single card. Solution : Note that rank is a character array struct Card because it can represent ‘10 ‘ only if it { is an array and not a character . char suit; char rank[3]; };

  4. Question 2 Part b 2b) Declare deck as an array of 52 cards in the main function. Write a function generateDeck to generate cards in order of suit, i.e. clubs followed by diamonds , then hearts and lastly spades; cards of the same suit are ordered by rank. CA C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK DA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK HA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK SA S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK The function prototype is as follows. void generateDeck(struct Card deck[]); For example, CA represents the Ace of Clubs , S10 represents the 10of Spades.

  5. Solution void generateDeck(struct Card deck[]); int main() { struct Card deck[52]; generateDeck(deck); printDeck(deck); return 0; } void generateDeck(struct Card deck[52]) { int i , j = 0; for(i=0;i<52;i++) { if(i<13) deck[i]. suit= ‘ C ‘; else if(i>=13 && i<26) deck[i]. suit= ‘ D ‘;

  6. else if(i>=26 && i<39) j++; deck[i]. suit= ‘ H ‘; if(j==13) else j=0; deck[i]. suit= ‘ S‘; } switch(j) } { case 0 : strcpy (deck[i]. Rank , ”A “); break; case 1 : strcpy (deck[i]. Rank , ”2 “); break; case 2 : strcpy (deck[i]. Rank , ”3 “); break; case 3 : strcpy (deck[i]. Rank , ”4 “); break; case 4 : strcpy (deck[i]. Rank , ”5 “); break; case 5 : strcpy (deck[i]. Rank , ”6 “); break; case 6 : strcpy (deck[i]. Rank , ”7 “); break; case 7 : strcpy (deck[i]. Rank , ”8 “); break; case 8 : strcpy (deck[i]. Rank , ”9 “); break; case 9 : strcpy (deck[i]. Rank , ”10 “); break; case 10 : strcpy (deck[i]. Rank , ”J “); break; case 11 : strcpy (deck[i]. Rank , ”Q “); break; case 12 : strcpy (deck[i]. Rank , ”K “); break; }

  7. Question 2 Part c 2c) Write a function printDeck to print the deck of cards according to the format in part 2b. void printDeck(struct Card deck[]); Verify that the card generation above is correct by using the main function as a driver and printing the deck of generated cards. Solution: void printDeck(struct Card deck[52]) { int i , j = 0; for(i=0;i<52;i++) { printf(“%c%s” , deck[i].suit, deck[i].rank ); printf(“ “); j++; if(j%13==0) printf(“\n”); } }

  8. OUTPUT: CA C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK DA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK HA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK SA S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK

More Related