A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

Can anyone help me with a c++ program I have to build a deck of cards ? and having some trouble...?

  • This Question is Open
  1. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    What have you done until know?

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hang on I'll try and upload my code...

    1 Attachment
  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I don't understand how I'm supposed to create a function that fills the "Card" struct for each card?.. do I have to use a for loop? but if so, how do I iterate it?

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Im just working on trying to fill the deck array for each card struct...

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Could you at least hint to me how to fill the struct Card array with a for loop?

  6. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Well, you know you want 52 cards. There are four suits and 13 cards for each suit. I'll just talk about the logic. There are several ways to do this. One way is to do an array of arrays. So you have 4 arrays, each with 13 cards. Something like: array = [['A', 2, 3 ,4...'K'], ['A', 2, 3 ,4...'K'], ['A', 2, 3 ,4...'K'], ['A', 2, 3 ,4...'K']]. Then, when you use it, you know that array[0] refers to a specific suit. The problem with this will come when you shuffle the deck, so I don't think this is the best way. Another way is to simply create an array of ['A','A','A','A'...'K','K'] and another array with the suits [1,2,3,4,1,2,3,4,1,2,3,4...3,4] and when you shuffle it, do the same to both arrays. You could even have an array suites = {1,2,3,4} and array faces = {1,2,3,4,5,6,7,8,9,10,11,12} and then work around it when shuffling Sorry for the syntax errors, I was just trying to cover the logic.

  7. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yes, the last method you talked about is how I am trying to approach it.. I understand that the suits and faces are inside the "Card" struct. But I'm not understanding how to create the for loops to match the integer to the suit?... because the I created an array of structs from Card ; called Card deck[52]; but I don't know how which loop to start with to fill it. Do i create the first loop, like, for(int i = 0; i < deck; i++) nested with another for loop; for(int k=0; k < suit; k++) nested with another for loop; for(int j =0; j < value; j++); deck[j] = j; ?

  8. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think two fors are enough. One that creates each suit, another one that creates every card inside

  9. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Or explain me why 3 fors

  10. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yes but how will I fill all 52 cards from "deck" array?

  11. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    because each suit is supposed to have 13 card values 1 -13 for hearts, 1-13 for spades, etc. all the way to 52 cards...

  12. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    but If I create an array called deck[52]. from the struct CARD... how do I first "set" the deck with each value assigned to each suit?... ensuring i'm using all numbers up to 52. ?

  13. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I don't know exactly how many for loops I would have to use but in what order do I assign the element. I don't even know how to start?

  14. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Do you want to repeat 4 times the same numbers, or go from 1 to 52 and just sat that from 1 to 13 is suit a, then from 14 to 26 I'm talking about suit b, etc

  15. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I just thought that you could simply do an array from 1 to 52, with those numbers. And with modulus and ranges it would work

  16. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    correct, going from 1-13, then 14- 26, for b etc. is how I want to do it. If I just stripped all the other junk away from my code and just started with my struct Card, my "deck array" (deck[52]), and my function to fill the array (bool set_deck(Card[]) ... and I start in my main and pass "deck" as my array value into set_deck(Card[]).. what for loop would I build first?

  17. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You can do it perfectly fine with 1 for loop that gets the values from 1 to 12, and repeat that loop 4 times. You can do it with while, too

  18. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    can you show me an example?

  19. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Mmm. i = 0 while i < 4 for(int idx=0; idx <= 13; idx ++) ADD IDX to ARRAY ENDFOR i = i + 1 ENDWHILE

  20. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    How does that look for the creation of the array?

  21. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    totally confusing. don't understand ?

  22. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    im filling an array inside the struct?...

  23. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Well thanks for the help. But Im going to have to move on. Thank you ]

  24. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.