You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given a deck containing N cards. While holding the deck facedown:
5
+
6
+
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).
7
+
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.
8
+
3. Pick up the deck from the table and repeat steps 1-2 until the deck is in the original order.
9
+
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.
10
+
11
+
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.
12
+
13
+
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.
14
+
*/
15
+
16
+
#include<stdio.h>
17
+
#include<stdlib.h>
18
+
#include<stdbool.h>
19
+
#include<unistd.h>
20
+
21
+
#defineMAX_TOTAL_PILES (5)
22
+
#defineNUM_PILES (3)
23
+
24
+
intpileNumberTable[3] = {3, 4, 5};
25
+
26
+
/* Linked List node */
27
+
typedefstructcard
28
+
{
29
+
intval;
30
+
structcard*next;
31
+
}card_t;
32
+
33
+
/* Linked list of card_t, or the head of a linked list of cards */
34
+
typedefstructpile
35
+
{
36
+
card_t*top_card;
37
+
}pile_t;
38
+
39
+
typedefstructdeck
40
+
{
41
+
intval;
42
+
structdeck*next;
43
+
}deck_t;
44
+
45
+
/* This array only stores the handler of each pile so that they can be
46
+
retrieved for cards each pile hold to form a deck */
47
+
pile_tpileArr[MAX_TOTAL_PILES];
48
+
49
+
50
+
/**
51
+
* @brief helper function to cleat/empty piles
52
+
*/
53
+
staticvoidresetPiles(void)
54
+
{
55
+
for(inti=0; i<MAX_TOTAL_PILES; i++){
56
+
pileArr[i].top_card->val=-1;
57
+
pileArr[i].top_card->next=NULL;
58
+
}
59
+
}
60
+
61
+
/**
62
+
* @brief add all the cards in pile to the deck
63
+
* @param[in] pile pile that contains cards
64
+
* @param[in/out] deck needs to be added with cards
0 commit comments