please don\'t change definition of a struct , prototypes or Parameters. this .h
ID: 3546854 • Letter: P
Question
please don't change definition of a struct , prototypes or Parameters.
this .h file i need your help in writing .c file and the main.
#ifndef _deck_h
#define _deck_h
#include "Card.h"
#define DECK_SIZE 52
/*
* Here is the definition of a struct for a deck of cards
* cards - an array of cards
* count - an int indicating the "logical" number of cards in the deck
*/
typedef struct
{
CardT cards[DECK_SIZE];
int count;
}DeckT;
/*
* Function prototypes for the Deck ADT
*/
/*
* Gives the deck its initial values
* Note: also initializes all the cards in the deck
*
* Parameters:
* a pointer to the deck to be initialized
* Return value:
* none
*/
void initDeck(DeckT *);
/*
* Shuffles all the cards currently in the deck
*
* Parameters:
* a pointer to the deck to be shuffled
* Return value:
* none
*/
void shuffleDeck(DeckT *);
/*
* Deals one card from the deck
*
* Parameters:
* a pointer to the deck
* Return value:
* a copy of the card being dealt
*/
CardT dealOneFromDeck(DeckT *);
/*
* Displays all cards currently in the deck
* Note: display one card per line
*
* Parameters:
* a pointer to the deck
* Return value:
* none
*/
void displayDeck(const DeckT *);
#endif
Explanation / Answer
// save as deck.c
// if u face any error. please let me know.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "deck.h"
void initDeck(DeckT *deck)
{
int i;
deck->count = 0;
for(i=0; i<DECK_SIZE; i++)
{
initCard(deck->cards[i],i);
}
}
void shuffleDeck(DeckT *deck)
{
int i;
int j;
printf("Printing deck values before shuffling ");
for(i=0; i<52; i++)
printf("%d ",deck->cards[i].value);
printf(" ");
srand(time(NULL));
for(i=51; i>=0; i--)
{
j = rand()%(i+1);
int temp = deck->cards[j].value;
deck->cards[j].value = deck->cards[i].value;
deck->cards[i].value = temp;
}
printf("Printing deck values After shuffling ");
for(i=0; i<52; i++)
printf("%d ",deck->cards[i].value);
}
CardT dealOneFromDeck(DeckT *deck)
{
int value = rand()%52;
return deck->cards[value];
}
void displayDeck(const DeckT *deck)
{
int i;
for(i=0; i<DECK_SIZE; i++)
displayCard(deck->cards[index]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.