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
  • Your task is to write the initDeck() function, which will be called at the start of...

    Your task is to write the initDeck() function, which will be called at the start of the program to initialize all of the cards data structures. The data structure definitions and a basic main program will be made available for you to use to develop your code.   The initDeck() function: /* Function: initDeck() * Purpose: iterate through entire deck and set to default values * Accepts: array of Cards (the deck) * Returns: void */ void initDeck(struct Card deck[]) {...

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • Cards are dealt at random and without replacement from a standard 52-card deck. What is the...

    Cards are dealt at random and without replacement from a standard 52-card deck. What is the probability that the third queen is dealt on the fifth card? (Round your answer to four decimal places.)

  • Cards are dealt at random and without replacement from a standard 52-card deck. What is the...

    Cards are dealt at random and without replacement from a standard 52-card deck. What is the probability that the third four is dealt on the fifth card? (Round your answer to four decimal places.)

  • Write a Java program that deals a five-card poker hand and then determines which of the...

    Write a Java program that deals a five-card poker hand and then determines which of the following hands are contained in it: high card, pairs, two pair, three of a kind, flush. Test only for those hands. Use the numbers from 0 to 51 to represent the cards of the poker deck. 1. To deal the cards, your main method should call a secondary method that selects a random card. The secondary method should accept 5 integers that represent the...

  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

  • We deal from a well shuffled 52-card deck. How many different ways can you deal 13...

    We deal from a well shuffled 52-card deck. How many different ways can you deal 13 cards so that there are no king cards among the first 12?

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