In: Computer Science
Use C to
Write a program that "cuts" a deck of 52 playing cards. Given a cut position, the process of cutting the deck refers to taking all cards before the cut position and moving them to the end of the deck. The order of the cards is maintained. For example, if the deck is cut at position 25, the first 25 cards of the deck are moved to become the last 25 cards of the deck. The new first card in the deck is card 26, and the new last card of the deck is card 25.
The deck of cards is represented as a 52-element integer array, with values 1 through 52 representing the different cards. The initial population of the deck is done for you.
Your program should accept a single integer input, which is the cut position. If the input is less than 1 or greater than 52, your program should print "ERROR" and exit. Otherwise, after performing the cut, your program should print out the new deck, with one card value on each line.
void cutDeck(int *deck, int cutPosition) { if(cutPosition < 1 || cutPosition > 52) { printf("Error!\n"); } else { // We need to perform n Rotations now. int rotations = 0; while(rotations++ < cutPosition) { int first = deck[0]; for(int i=1; i<52; i++) { deck[i-1] = deck[i]; } deck[51] = first; } // Now print the deck. for(int i=0; i<52; i++) { printf("%d ", deck[i]); } printf("\n"); } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.