Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to create a deck of cards and the suit that each card has using a struct

ID: 3813500 • Letter: I

Question

I need to create a deck of cards and the suit that each card has using a struct and linked lists to record, save and pring the deck of cards. what would that look like?

Explanation / Answer

#include #include typedef struct _card { int suit; int value; } card; typedef struct _deck { int num_cards; card **cards; } deck; card *make_card(int suit, int value) { card *newCard = malloc(sizeof(card)); newCard->suit = suit; newCard->value = value; return newCard; } deck *make_standard_deck( void ) { deck *newDeck = malloc( sizeof(deck) ); newDeck->num_cards = 52; newDeck->cards = malloc( 52 * sizeof(card *) ); int index = 0; for ( int suit = 0; suit < 4; suit++ ) for ( int value = 1; value cards[index++] = make_card( suit, value ); return newDeck; } int main( void ) { int i; deck *stdDeck = make_standard_deck(); for ( i = 0; i num_cards; i++ ) printf( "suit=%d value=%2d ", stdDeck->cards[i]->suit, stdDeck->cards[i]->value ); /* free the deck when we're done with it */ for ( i = 0; i num_cards; i++ ) free( stdDeck->cards[i] ); free( stdDeck->cards ); free( stdDeck ); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote