Answer to Question #161637 in C for Basit

Question #161637

Question 1 

You are given a deck containing N cards. While holding the deck facedown: 1. Deal all the cards facedown onto a table into Y piles like you would if you were playing with a group of people (i.e. card 1 to P1, card 2 to P2, ..., card Y to PY, card Y + 1 to P1, etc). 2. Combine all the piles into a deck by placing P1 onto P2, then P1+P2 onto P3, and so on. This is a round. 3. Pick up the deck from the table and repeat steps 1-2 until the deck is in the original order. 4. For each round, vary the pile count according to a repeating pattern. Start with 3 piles, then 4, then 5, then loop back to 3, then 4 and so on.

* Write a program to determine how many rounds it will take to put a deck back into the original order. This will involve creating a data structure to represent the order of the cards. Do not use an array. This program should be written in C only. It should take a number of cards in the deck as a command line argument and write the result to stdout. Please ensure the program compiles and runs correctly (no pseudo-code). This isn't a trick question; it should be fairly straightforward. Bonus: Output how many rounds should be completed before the deck is adequately shuffled from the original deck for a person who is casually playing a game with cards. Provide your methodology in a comment block. 


1
Expert's answer
2021-02-06T10:09:58-0500
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
int main()
{
    int in_hand[NUM_SUITS][NUM_RANKS] = { 0 };
    int num_cards = 1, rank, suit;
    const char rank_code[] = { '2','3','4','5','6','7','8','9','t','j','q','k','a' };
    const char suit_code[] = { '\x03','\x04','\x05','\x06' };
    
   
    srand(time(0));
    printf("Your cards : \n");
    while (num_cards <= 27)
    {
        suit = rand() % NUM_SUITS; 
        rank = rand() % NUM_RANKS;  
                                   
        if (!in_hand[suit][rank])
        {
 
        
            if (num_cards <= 9)
            {
                if (num_cards == 1)
                {
                    printf("First player: ");
                }
                printf("%c%c  ", rank_code[rank], suit_code[suit]);
                if (num_cards == 9)
                {
                    printf("\n");
                }
            }
 
 
            if (num_cards >= 10 && num_cards <= 18) 
            {
                if (num_cards == 10)
                {
                    printf("Second player: ");
                }
                printf("%c%c  ", rank_code[rank], suit_code[suit]);
                if (num_cards == 18)
                {
                    printf("\n");
                }
            }
 
 
            if (num_cards>18) 
            {
                if (num_cards == 19)
                {
                    printf("Third player: ");
                }
                printf("%c%c  ", rank_code[rank], suit_code[suit]);
                if (num_cards == 27)
                {
                    printf("\n");
                }
            }
            
 
            in_hand[suit][rank] = 1; 
            num_cards++;
            
        }
    
    }
    printf("\n");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS