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

Please post the solution and the output. Please save the program with the name \

ID: 3859111 • Letter: P

Question

Please post the solution and the output. Please save the program with the name 'cards.c'. This problem is kind of similar to the exercise from Chapter 7 (Structing the Data) from C by Discovery.

a) Write a structure (struct card) that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 6, 7, 2) of each card. Note that a deck of playing cards can be represented as an array declared as struct card deck[52];

b) Write a function that will perform a perfect shuffle on a deck of cards represented using the data structures from part a. In a perfect shuffle, the deck is broken exactly in half and rearranged so that the first card is followed by the 27th card, followed by the second card, followed by the 28th card, and so on.

c) Write a program that tests how many perfect shuffles are necessary to return the deck to its original configuration.

Explanation / Answer

#include <stdio.h>

struct card {
   char suit[10];
   char rank[3];
};


struct card deck[52];


init() {
   int i,j;
   for (i = 0; i < 4; i++)
       for (j = 0; j < 13; j++) {
           switch(i) {
           case 0:
               strcpy(deck[i*13+j].suit, "club");
               break;
           case 1:
               strcpy(deck[i*13+j].suit, "diamond");
               break;
           case 2:
               strcpy(deck[i*13+j].suit, "heart");
               break;
           case 3:
               strcpy(deck[i*13+j].suit, "spade");
           }

           switch (j) {
               case 0:
                   strcpy(deck[i*13+j].rank, "A");
                   break;
               case 1:
                   strcpy(deck[i*13+j].rank, "2");
                   break;
               case 2:
                   strcpy(deck[i*13+j].rank, "3");
                   break;
               case 3:
                   strcpy(deck[i*13+j].rank, "4");
                   break;
               case 4:
                   strcpy(deck[i*13+j].rank, "5");
                   break;
               case 5:
                   strcpy(deck[i*13+j].rank, "6");
                   break;
               case 6:
                   strcpy(deck[i*13+j].rank, "7");
                   break;
               case 7:
                   strcpy(deck[i*13+j].rank, "8");
                   break;
               case 8:
                   strcpy(deck[i*13+j].rank, "9");
                   break;
               case 9:
                   strcpy(deck[i*13+j].rank, "10");
                   break;
               case 10:
                   strcpy(deck[i*13+j].rank, "J");
                   break;
               case 11:
                   strcpy(deck[i*13+j].rank, "Q");
                   break;
               case 12:
                   strcpy(deck[i*13+j].rank, "K");
                   break;
           }
          
       }
}

void suffle() {
   struct card deckTmp[52];
   int pos,i;
   for (i = 0; i<26; i++) {
       deckTmp[i*2] = deck[i];
       deckTmp[i*2+1] = deck[26 + i];
   }
   for (i = 0; i<52; i++) {
       //printf("deck[%d] = %s%s. ",i,deck[i].suit,deck[i].rank);
       deck[i] = deckTmp[i];
       //printf("Coping %s%s to loc %d ",deck[i].suit,deck[i].rank, i);
   }
}

int is_in_order() {
   int i,j;
   for (i = 0; i < 4; i++)
       for (j = 0; j < 13; j++) {
           switch(i) {
           case 0:
               if (strcmp(deck[i*13+j].suit,"club"))
                   return 0;
               break;
           case 1:
               if (strcmp(deck[i*13+j].suit,"diamond"))
                   return 0;
               break;
           case 2:
               if (strcmp(deck[i*13+j].suit, "heart"))
                   return 0;
               break;
           case 3:
               if (strcmp(deck[i*13+j].suit, "spade"))
                   return 0;
           }

           switch (j) {
               case 0:
                   if (strcmp(deck[i*13+j].rank, "A"))
                       return 0;
                   break;
               case 1:
                   if (strcmp(deck[i*13+j].rank, "2"))
                       return 0;
                   break;
               case 2:
                   if (strcmp(deck[i*13+j].rank, "3"))
                       return 0;
                   break;
               case 3:
                   if (strcmp(deck[i*13+j].rank, "4"))
                       return 0;
                   break;
               case 4:
                   if (strcmp(deck[i*13+j].rank, "5"))
                       return 0;
                   break;
               case 5:
                   if (strcmp(deck[i*13+j].rank, "6"))
                       return 0;
                   break;
               case 6:
                   if (strcmp(deck[i*13+j].rank, "7"))
                       return 0;
                   break;
               case 7:
                   if (strcmp(deck[i*13+j].rank, "8"))
                       return 0;
                   break;
               case 8:
                   if (strcmp(deck[i*13+j].rank, "9"))
                       return 0;
                   break;
               case 9:
                   if (strcmp(deck[i*13+j].rank, "10"))
                       return 0;
                   break;
               case 10:
                   if (strcmp(deck[i*13+j].rank, "J"))
                       return 0;
                   break;
               case 11:
                   if (strcmp(deck[i*13+j].rank, "Q"))
                       return 0;
                   break;
               case 12:
                   if (strcmp(deck[i*13+j].rank, "K"))
                       return 0;
                   break;
           }
       }
   return 1;
}


int main() {
   int i = 0;
   init();
   do {
       suffle();
       i++;
   }while(!is_in_order());      
   printf("Initaial ordering is restored after %d suffling ",i);
}

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