Hello everyone, Thank you so so much if I get the right answer. Please help me w
ID: 3787755 • Letter: H
Question
Hello everyone, Thank you so so much if I get the right answer. Please help me with all parts of the program, I don't mind if it is separate programs. I need this by the end of today. here it is
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];
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 then
Write a program that tests how many perfect shuffles are necessary to return the deck to its original configuration. Here is my code, if it will help
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct deck
{
char suit[9];
char rank[6];
}
card[52];
char temp;
int main()
{
char suits[4][9]={"hearts","diamonds","spades","clubs"};
char cards[13][6]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
int i,j,k;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
{for(k=0;k<9;k++)
card[i*13+j].suit[k]=suits[i][k];
for(k=0;k<6;k++)
card[i*13+j].rank[k]=cards[j][k];
}
printf("deck before shuffle ");
for(i=0;i<52;i++)
printf("%s %s ",card[i].rank,card[i].suit);
getch();
return 0;
}
that is only part a please help fast
Explanation / Answer
#include <iostream>
using namespace std;
struct card{
string suit;
string rank;
};
class deck{
public:
card originalDeck[52];
card shuffledDeck[52];
int timesShuffled;
void initialize(){
string suits[]={"Club","Diamond","Heart","Spade"};
string ranks[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
int k=0;
for(int m=0; m<4; m++){
for(int n=0; n<13; n++){
card temp;
cout<<k<<endl;
temp.suit=suits[m];
temp.rank=ranks[n];
shuffledDeck[k]=temp;
originalDeck[k]=temp;
k++;
}
}
timesShuffled=0;
}
void printCards(){
for (int i=0; i<52; i++){
cout<<shuffledDeck[i].rank<<" "<<shuffledDeck[i].suit<<endl;
}
}
void perfectShuffle(){
card tempDeck[52];
int j=0;
for(int i=0; i<26; i++){
tempDeck[j] = shuffledDeck[i];
tempDeck[j+1] = shuffledDeck[i+26];
j=j+2;
}
for(int i=0; i<52; i++){
shuffledDeck[i]=tempDeck[i];
}
timesShuffled++;
}
int minPerfectSuffle(){
int q=0;
while(true){
cout<<q<<endl;
q++;
for(int i=0; i<52; i++){
if((originalDeck[i].rank==shuffledDeck[i].rank) && (originalDeck[i].suit==shuffledDeck[i].suit)){
if(i==51){
return timesShuffled;
}
}
else{
break;
}
}
perfectShuffle();
timesShuffled++;
}
return 0;
}
};
int main(){
deck trump;
trump.initialize();
trump.printCards();
cout<<"Line 1: ---------------------------------------------"<<endl;
trump.perfectShuffle();
trump.printCards();
cout<<"Line 2: ---------------------------------------------"<<endl;
int t=trump.minPerfectSuffle();
trump.printCards();
cout<<"Line 3: ---------------------------------------------"<<endl;
cout<<"Mininum number of Shuffled required : "<<t<<endl;
// printCards(deck);
cout<<"---------------------------------------------"<<endl;
// perfectShuffle(deck);
// printCards(shuffled);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.