Passing two Dimensional Arrays

This is a discussion on Passing two Dimensional Arrays within the C++ Programming forums, part of the General Programming Boards category; I am currently working on a black jack game for a school project. I decided to set up the deck ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Passing two Dimensional Arrays

    I am currently working on a black jack game for a school project. I decided to set up the deck to be used as a two dimensional array to symbolis the card amount and the card's suit. I currently believe I am running into trouble passing the arrays through certain functions though. I believe I have the general set up correct but my main question is if you can pass a const in the second half of the array as the number it. Here is how I currently have it set up.
    Code:
    const int suit_size = 3;
    const int number_size = 12;
    
    int setup(int[][suit_size], int, int*);
    
    setup(deck[][suit_size], number_size, &player.money);
    
    int deck[number_size][suit_size];
    
    int setup(int deck[][suit_size], int number, int *money)
    {
        int j = 0;
        for(int i = 0; i <= suit_size ; i++){
            for(j = 0; j <= number; j++){
                deck[j][i] = 0;
            }
        }
        *money = 100;
    }
    This is not all the code I have just the code that should effect this question. My main question is it allowed to pass the area size in this fashion. If you have any other suggestions though that you believe might help me feel free to make any suggestions.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why is the suit_size 3? You only have 3 suits?
    And the number_size is 12? Only 12 values?

    Anyway, I doubt the two-dimensional array approach can work. A deck of cards is a one-dimensional array. Each element in the array has two pieces of information associated with it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Well I have it as suit_size of 3 because the array would then have array elements of 0, 1, 2, 3 so gives me 4 overall and same thing with the number_size.

    And I am using a two dimensional array just because it was just easier for me to think of the cards that way and then I can use the value to add to the total amount the player has.

    Also in case it is needed to be taken in consideration the error I am getting is
    "eror: expected primary-expression before '] ' token"
    And this error is pointing to the line when I call the function setup.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    [3] gives you a total of 3 elements, numbered 0, 1, 2. You want it to be [4]. And the other should be [13].

    I can't imagine how a 2d array will work. In fact, I guarantee that it won't.

    Think about it. A deck is a one-dimensional array. It's more like:
    Code:
    struct Card {
        int value;
        int suit;
    };
    Card deck[52];

    Your current code is screwed up because of the weird prototype-like statements. It should be more like:
    Code:
    const int suit_size = 4;
    const int number_size = 13;
     
    int deck[number_size][suit_size];
    
    int setup(int deck[][suit_size], int number, int *money);
    
    int setup(int deck[][suit_size], int number, int *money)
    {
        int j = 0;
        for(int i = 0; i <= suit_size ; i++){
            for(j = 0; j <= number; j++){
                deck[j][i] = 0;
            }
        }
        *money = 100;
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    If you do not mind me asking why would a two dimensional array not work?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    It can work, but it is just a more difficult way of doing things compared to using a struct/class to represent a card.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    It can work, but it is just a more difficult way of doing things compared to using a struct/class to represent a card.
    I honestly can't see how it would work. I'll see if I can get it to work, though. Hmmmm.....

    Oh, and I forgot to change your loop conditions from <= to <.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is what your "deck" will look like when it's initialized with zeroes (after setup is called).

    . 0 1 2 3 4 5 6 7 8 9 10 11 12
    0 0 0 0 0 0 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0 0 0 0 0 0 0
    3 0 0 0 0 0 0 0 0 0 0 0 0 0

    Doesn't look much like a deck to me.

    How will you shuffle it?
    Where is the "top" card?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Yeah I guess it would be easier to use a struct and I think I could find a way to where only one of each card will be drawn for the whole deck. What I was trying to do is the program would pick a random number for the suit and the value then I would put those two numbers into the array and test if I have used it yet then either redraw a new set if it has been used or mark that set as used and add the value to the total.

    Edit: I apologize I should of explain more my logic behind using the 2-d array, I have had a long day and not able to think the easiest on some things. I was actual going to change that int to a bool and just use it as a test to see if that value has already been used or not.
    Last edited by thadis_4; 04-18-2012 at 09:42 PM.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Oh, I see. I hadn't thought of that. But once you get down to only a few cards left, you may be picking a lot of random numbers until you get a free card.

    The usual approach is to have a 1d array. I usually just use an array of ints initialized with the numbers 0 to 51. That can be shuffled with only 51 calls to rand(), which is nice. Then you just keep an index to the top card which is incremented after a card is drawn. The suit and value are interpreted like so:
    Code:
    int deck[52]; // inited with 0 to 51
    int top = 0;
    
    int card = deck[top++];
    
    int value = card % 13 + 1;  // gives 1 to 13
    int suit = card / 13;  // gives 0 to 3; interpret those as you may
    Last edited by oogabooga; 04-18-2012 at 10:29 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Oh I did not think about what might happen if it just never ended up getting the correct set for the final card.

    I see what you are saying though how would you have rand() use only cards that have not been drawn previously?

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by thadis_4 View Post
    Oh I did not think about what might happen if it just never ended up getting the correct set for the final card.
    You'd eventually get it!

    Quote Originally Posted by thadis_4 View Post
    I see what you are saying though how would you have rand() use only cards that have not been drawn previously?
    You would only use rand() to "shuffle" the cards before each game. Once the cards are shuffled you just step through them one at a time, just like a real deck of cards. The standard shuffle algorithm goes like this:
    Code:
    void shuffle() {
        for (int i = 51; i > 0; i--) {
            int r = rand() % (i + 1);
            int t = cards[i]; cards[i] = cards[r]; cards[r] = t;  // swap cards[i] and cards[r]
        }
        top = 0;
    }
    This would be in a class called, say, Deck, and assumes that cards[] has been initialized with the integers 0 to 51. The int top is inited to 0 by the shuffle routine and is incremented each time you "deal" out another card.

    Try setting up a cards array initialized with the numbers 0 to 51 and do that shuffle routine on it and then print it out. You'll see that it's nicely shuffled up. (Remember to call srand((unsigned)time(0)) as the first statement in main to init the random number generator.)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Ok I think I understand what you are saying though it took me a little while to understand how you used the modulus. Does it work that way because you will always have some sort of remainder that will be under 'i' because of the (i+1)?

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by thadis_4 View Post
    Does it work that way because you will always have some sort of remainder that will be under 'i' because of the (i+1)?
    That's right. n % 13, for example, will always be between 0 and 12, inclusive.

    BTW, I've noticed an error in my post#10 above. I originally had suit=card/4. But it should be suit=card/13. I've changed it in the original post.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Thank you so much for all the help you have give me, think it really helps though I have a feeling I will need to know classes the way you explained it saying shuffle() does not have a return or a argument so I guess I have to wait a little till I learn about classes to start some of this. Well think I only have one more chapter till then. Thank you again for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing two-dimensional arrays to functions
    By stdq in forum C Programming
    Replies: 7
    Last Post: 02-05-2012, 10:45 AM
  2. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 11:25 AM
  3. Passing 2 dimensional arrays to functions
    By homeyg in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2005, 03:16 PM
  4. passing two dimensional arrays
    By Nova_Collision in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 01:47 PM
  5. Passing 2 dimensional Arrays to functions
    By aresashura in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2001, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21