# Passing two Dimensional Arrays

• 04-18-2012
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.
• 04-18-2012
oogabooga
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.
• 04-18-2012
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.
• 04-18-2012
oogabooga
[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; }```
• 04-18-2012
If you do not mind me asking why would a two dimensional array not work?
• 04-18-2012
laserlight
It can work, but it is just a more difficult way of doing things compared to using a struct/class to represent a card.
• 04-18-2012
oogabooga
Quote:

Originally Posted by laserlight
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 <.
• 04-18-2012
oogabooga
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?
• 04-18-2012
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.
• 04-18-2012
oogabooga
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```
• 04-18-2012
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?
• 04-18-2012
oogabooga
Quote:

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:

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.)
• 04-18-2012
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)?
• 04-18-2012
oogabooga
Quote: