
Malik S.
asked 02/08/21C++ programming question for Deck of cards
PROGRAMMING LANGUAGE: C++
Q.No.1:
Deck of card contains 52 cards in total and catogrised in 4 different categories named as Heart, Diamond, Spade and Club. Each category contains 13 cards. Each category having cards denomination [2,3,4,5,6,7,8,9,10, J(Jack), Q(Queen), K(King), A(Ace)].
Suppose 4 cards are missing (we are unknown about missing cards) and remaining 48 mixed cards are distributed arbitrarily/randomly to 4 different players. Write down a C++ program to demonstrate the above-mentioned problem by adding three functions other than main() function:
A) First one is Sort() function which pack all these cards into one packet by arranging them in a sequence of all Spade card comes 1st in asending order, then all Club cards, then all Heart cards and at the end all Diamond cards.
B) Second one is displayCards() function which displays all sorted cards achieved in Part-A in the following format:
The Pack Cards are:
Card1: Spade 2
Card2: Spade 3
..................................
..................................
Card48: Diamond A
C) Third one is findMissing() function which displays the category and number of missing cards in the following format e.g.:
The Missing Cards of 20-Arid-yourNo:
Card1: Spade 4
Card2: Heart J
Card3: Club 10
Card4: Diamond 3
Note: You have to add the screen shot of your output of missing cards.
D) Fourth one is findMissingFromFile() function which reads all 48 sorted cards data from file "your_Arid_No.txt" (assume file is already available with all the sorted data e.g. Spade 2) and find & write missing cards into another file "missingCards.txt" in the following format:
Card1 Spade 4
Card2 Heart J
Card3 Club 10
Card4 Diamond 3
//Here is the main function for your help
int main()
{
Card Player1[12]={{D,4},{H,2},{S,A},.............};
//Incomplete values just to give idea, remaining you have to fill by yourself.
//{D,4} here D is for Diamond type and 4 is the card number in diamond category.
Card Player2[12]={};
Card Player3[12]={};
Card Player4[12]={};
Card Pack[48];
//Call of sort() Function, which sort all 4 player's cards in Pack array. You can assign 'N' as a null in a player array for the card number, if you picked that card and placed in Pack array.
//Call of display() function, which displays all sorted cards placed in Pack array
//Call of findMissing() function
//Call of findMissingFromFile() function
return 0;
}
1 Expert Answer

Patrick B. answered 02/09/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SPADE (0)
#define CLUB (1)
#define HEART (2)
#define DIAMOND (3)
#define JACK (11)
#define QUEEN (12)
#define KING (13)
#define ACE (14)
#define INVALID_SUIT (-1)
#define INVALID_DENUM (0)
typedef struct _TCard
{
int denum;
int suit;
} * TCard;
#define CARD_SIZE (sizeof(struct _TCard))
typedef struct _TDeck
{
struct _TCard cards[52];
} * TDeck;
Deck_Init(TDeck deck)
{
for (int suitLoop=SPADE; suitLoop<=DIAMOND; suitLoop++)
{
for (int denumLoop=2; denumLoop<=ACE; denumLoop++)
{
struct _TCard curCard;
curCard.denum = denumLoop;
curCard.suit = suitLoop;
memcpy(&deck->cards[suitLoop*13+(denumLoop-2)],&curCard, CARD_SIZE);
}
}
}
int Card_Compare(const void * rec1, const void * rec2)
{
int iReturn=0;
TCard cardPtr1 = (TCard)rec1;
TCard cardPtr2 = (TCard)rec2;
if (cardPtr1->suit == cardPtr2->suit)
{
iReturn = cardPtr1->denum - cardPtr2->denum;
}
else
{
iReturn = cardPtr1->suit - cardPtr2->suit;
}
return(iReturn);
}
Deck_Sort(TDeck deck)
{
qsort(deck,52,CARD_SIZE,Card_Compare);
}
Card_Invalidate(TCard card)
{
card->denum = INVALID_DENUM;
card->suit = INVALID_SUIT;
}
inline bool Card_IsValid(TCard card)
{
return( ((card->denum!=INVALID_DENUM) || (card->suit!=INVALID_SUIT) ));
}
Card_Display(TCard card)
{
int curSuit = card->suit;
int curDenum = card->denum;
switch (curDenum)
{
case JACK: { cout << "J "; break; }
case QUEEN: { cout << "Q "; break; }
case KING: { cout << "K " ; break; }
case ACE: { cout << "A " ; break; }
case INVALID_DENUM: { break; }
default: { cout << curDenum << " "; break; }
} //switch denum
switch (curSuit)
{
case SPADE: { cout << "S" << endl; break; }
case CLUB: { cout << "C" << endl; break; }
case HEART: { cout << "H"<< endl; break; }
case DIAMOND: { cout << "D" << endl; break; }
case INVALID_SUIT: { break; }
} //switch suit
}
Deck_Display( TDeck deck, char * strMsg=NULL)
{
if (strMsg!=NULL)
{
cout << "********************************************************" << endl;
cout << strMsg << endl;
}
cout << "****************************************************************" << endl;
for (int iLoop=0; iLoop<52; iLoop++)
{
Card_Display(&deck->cards[iLoop]);
} //for loop
}
Deck_Shuffle(TDeck deck)
{
for (int iLoop=0; iLoop<52; iLoop++)
{
int random_index = rand()%52;
struct _TCard tempCard;
memcpy(&tempCard,&deck->cards[iLoop],CARD_SIZE);
memcpy(&deck->cards[iLoop],&deck->cards[random_index],CARD_SIZE);
memcpy(&deck->cards[random_index],&tempCard,CARD_SIZE);
}
}
int Deck_findMissingCards(TDeck deck, TCard missingCards, int numMissingCards)
{
srand(time(NULL));
int iIndexPos=0;
Deck_Sort(deck); //invalid cards move to the front...
TCard curCardPtr = &deck->cards[numMissingCards];
struct _TDeck goodDeck;
Deck_Init(&goodDeck);
int goodDeckIndex=0;
while (numMissingCards>0)
{
if (memcmp(&goodDeck.cards[goodDeckIndex],curCardPtr,CARD_SIZE)!=0) //mismatch
{
printf("mismatch found \n");
numMissingCards--;
memcpy(&missingCards[iIndexPos++],&goodDeck.cards[goodDeckIndex++],CARD_SIZE);
}
else
{
goodDeckIndex++;
curCardPtr++;
} //mismatch
} //while
}
/**************************
file contains:
# of cards missing <--- first line
array of cards : denum suit
********************************/
Deck_findMissingFromFile(char * filename)
{
FILE * fptr;
struct _TDeck deck;
TCard missingCards;
int numMissingCards;
int N;
int iReturn=0;
char msgBuff[75];
fptr = fopen(filename,"r");
if (fptr!=NULL)
{
fscanf(fptr,"%d",&numMissingCards); printf(" %d cards missing from file \n",numMissingCards);
N = 52-numMissingCards;
for (int iLoop=0; iLoop<numMissingCards; iLoop++) { Card_Invalidate(&deck.cards[iLoop]); }
missingCards = (TCard) malloc(CARD_SIZE*numMissingCards);
for (int iLoop=numMissingCards; iLoop<52; iLoop++)
{
fscanf(fptr,"%d %d",&deck.cards[iLoop].denum,&deck.cards[iLoop].suit);
}
fclose(fptr);
Deck_findMissingCards(&deck,missingCards,numMissingCards);
sprintf(msgBuff,"Compromised Deck from File: %d cards missing",numMissingCards);
Deck_Display(&deck,msgBuff);
cout << "-----------------------" << endl;
cout << " The missing cards are : " << endl;
for (int iLoop=0; iLoop<numMissingCards; iLoop++)
{
Card_Display(&missingCards[iLoop]);
}
free(missingCards);
}
else
{
iReturn=-1;
printf("error opening input file \n");
}
return(iReturn);
}
int main()
{
struct _TDeck deck;
Deck_Init(&deck);
Deck_Display(&deck);
cout << "shuffling....." << endl;
Deck_Shuffle(&deck);
Deck_Display(&deck,(char*)"SHUFFLED");
cout << "sorting..." << endl;
Deck_Sort(&deck);
Deck_Display(&deck,(char*)"SORTED");
struct _TCard missingCards[4];
for (int iLoop=0; iLoop<4; iLoop++)
{
int random_index=rand()%52;
Card_Invalidate(&deck.cards[random_index]);
}
Deck_findMissingCards(&deck,missingCards,4);
Deck_Display(&deck,(char*)"Compromised Deck: 4 cards missing ");
cout << "-----------------------" << endl;
cout << " The missing cards are : " << endl;
for (int iLoop=0; iLoop<4; iLoop++)
{
Card_Display(&missingCards[iLoop]);
}
Deck_findMissingFromFile((char*)"E:\\deck.dat");
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Source code uploaded to RESROUCES section under this link..02/09/21