Question

Please help me with this c++ segment of code, I am a newbie. I need help...

Please help me with this c++ segment of code, I am a newbie.

I need help to shuffle a deck of 52 cards using a stack of an array.

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

PLEASE UPVOTE IF YOU LIKE THIS ANSWER.

CODE:

#include <iostream>
#include <cstdlib>
using namespace std;
void shuffle_deck(int card[], int n)      // function to shuffle deck cards
{
    for (int i=0; i<n ;i++)
    {
        int random = i + (rand() % (52 -i));     // randomising the values

        swap(card[i], card[random]);             // swapping previous value with random value
    }
}
int main()
{
    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52};
    int size = sizeof(array) / sizeof(array[0]);   // array size declaration
    shuffle_deck(array, size);                     // calling shuffle function
    for (int i=1; i<=size; i++)
   {
        cout << array[i] << " ";                   // printing random or shuffled values
    }
    cout << endl;

    return 0;
}


OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Please help me with this c++ segment of code, I am a newbie. I need help...
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
  • I need this to be implements in c++ using a stack of arrays with classes I...

    I need this to be implements in c++ using a stack of arrays with classes I want to output a deck of cards that's randomly distributed between two players meaning each player shall get 26 cards. I also want to add up each players pile of cards and the player with the highest sum wins Please help me with this task I am a newbie

  • I want to insert a standard 52 card deck into a stack of arrays and then...

    I want to insert a standard 52 card deck into a stack of arrays and then randomly output the deck of cards until the deck is empty. I need this to be implement in c++ using a class of a stacked array please someone help me :(

  • I need help with this c++ code. This is my first computer science course. I have...

    I need help with this c++ code. This is my first computer science course. I have not learned arrays but I am learning vectors. I am also required to write this code without do loops or any other form of advanced coding technique. Please help me figure out this code using the basic coding techniques that you would find in an introduction to cs. Thank you for your help, I don't even know where to start :)

  • Can someone please help me. I am having trouble writing the following SQL query: "Retrieve the...

    Can someone please help me. I am having trouble writing the following SQL query: "Retrieve the name and country of the player with the most yellow cards in the 2014 world cup." The player and country are in the player table, and the yellow cards are in the player_cards table. The tables can be connected on player_id. The problem I am having is that there are 3 players that have 3 yellow cards, not just a single player. (3 yellow...

  • I am just curious about this question... please can you answer with applying indent and space...

    I am just curious about this question... please can you answer with applying indent and space clearly. Furthermore, can you make answer shortly as possible..? This is a python question Question 6.34 The two-player card game war is played with a standard deck of 52 cards. A shuffled deck is evenly split among the two players who keep their decks face-down. The game consists of battles until one of the players runs out of cards. In a battle, each player...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...

  • I am in need of some help with UNIX/Linux, particularly sed and awk: Please help me...

    I am in need of some help with UNIX/Linux, particularly sed and awk: Please help me Display from /etc/passwd a list of users and their shells for those using the Korn shell or Bash shell. Order the output by the absolute pathname of the shell used. Thank you.

  • PLEASE solve and show formulas used in Excel! I am so slow at this! thank you!...

    PLEASE solve and show formulas used in Excel! I am so slow at this! thank you! :) Use the cells on this sheet to make calculations. Calculate the probability of pulling 4 straight Kings or Queens one after the other from a deck of 52 cards. Calculate the probability of pulling 2 face cards (King or Queen or Jack) in 3 tries from a deck of 52 cards.

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