Question

Please write in C Programming Your task is to "deal" a card from the card deck....

Please write in C Programming

Your task is to "deal" a card from the card deck. standard 52 deck

/* Function: dealCard()

Purpose: Pick a card at random from the deck

Accepts: struct Card * -- pointer to array of cards (deck)

Returns: struct Card * -- pointer to the dealt card*/

struct Card *dealCard(struct Card *deck) {

// Iterate through the entire deck to make sure there is at least one card that has NOT been dealt

// If all cards are dealt, return a 0

// Generate a random number to pick a card randomly from the deck

// If the card is not available (has been dealt), then try another random number (loop until you get an available card)

// set the card as "dealt" and return it

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h> //for printf and scanf
#include<stdlib.h> //for srand function
#include<time.h> //for time function
#include<stdbool.h> //for bool datatype
//struct card with attributes ch represents card number and dealt to know card status
struct card{
int ch;
bool dealt;
};

//returns 0 if all cards are taken, else returns the randomly generated card
struct card *dealCard(struct card *deck)
{
int i;
srand(time(0)); //for generating distinct numbers
for(i=0;i<52;i++){
if(deck[i].dealt==false)
break;
}
if(i==52)
return 0;
while(1){
i = rand()%52;
if(deck[i].dealt==false){
deck[i].dealt=true;
break;
}
}
return (deck+i);
}

//main function implementation
int main()
{
struct card deck[52],*ptr;
int i;
for(i=0;i<52;i++){ //sets the intial values for deck
deck[i].ch=i+1;
deck[i].dealt=false;
}


//calls the function for 11 times and prints the returned card number
for(i=0;i<=10;i++){
ptr=dealCard(deck);
printf("Card %d\n",ptr->ch);
}
return 0;
}

//output screenshot

//any query, post in the comment section

Add a comment
Know the answer?
Add Answer to:
Please write in C Programming Your task is to "deal" a card from the card deck....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT