Why is this number in my 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
#include <iostream>

// Self explainatory prototypes
void loadDeck(unsigned short card[][2], const unsigned int DECKSIZE);
void dispDeck(unsigned short card[][2], const unsigned int DECKSIZE);
void shuffleDeck(unsigned short card[][2], const unsigned int DECKSIZE);

const unsigned short DECKSIZE = 52; // Deck has 52 cards
const unsigned short SUITS = 4; // Deck has 4 different suits
const unsigned short FACES = 13; // Cards have 13 different faces

// Array holding the standard suits of the cards
const std::string STDSUIT[4] = {"Clubs", "Diamonds", "Hearts", "Spades"};
// Array holding the standard faces of the cards
const std::string STDFACE[13] = {"Ace", "Two", "Three", "Four", "Five", "Six",
                                 "Seven", "Eight", "Nine", "Ten", "Jack",        "Queen",
                                 "King"};

int main(void)
{
    unsigned short card[DECKSIZE][2] = {0}; // Entire array initialized to 0
    
    loadDeck(card, DECKSIZE); // Call of loadDeck fuction using card array & DECKSIZE
    std::cout << "Initialized Deck:" << std::endl;
    
    
}

void loadDeck(unsigned short card[][2], const unsigned int DECKSIZE) //why 2?
{
    // Loop runs until c = 52 (DECKSIZE)
    for(unsigned short c = 0; c < DECKSIZE; ++c)
    {
        // Position c, 0 is the remainder of c value divided by 13 (# of faces)
        card[c][0] = c % 13;
        // Position c, 1 is the division of c value divided by 13 (# of faces)
        card[c][1] = c / 13;
    }
}
void dispDeck(unsigned short card[][2], const unsigned int DECKSIZE);
void shuffleDeck(unsigned short card[][2], const unsigned int DECKSIZE);



Hey guys, for some reason my code brackets aren't working so I have to just paste my code like above...

Anyways, my code is not complete but I am working on a 2 dimensional array to display and shuffle a deck of cards. I was working with my professor and I wrote my function prototypes just as he did... I am curious why there is a 2 inside the second dimension of my arrays in the prototypes and the declarations.

With that question looming, I guess an answer for that will also lead to a self explanatory answer for why we wrote the loadDeck function the way we did. I am a little rusty on C++ and I am trying to get back into my groove..

Thanks in advance for any help!
Last edited on
for some reason my code brackets aren't working so I have to just paste my code like above..

Learn to use code tags here:
http://www.cplusplus.com/articles/jEywvCM9/
You will find people much more willing to help you when you use code tags.

I am curious why there is a 2 inside the second dimension of my arrays in the prototypes and the declarations.

The compiler needs to know the second dimension. The first dimension is not required.
Consider if the card argument was declared as short card[][]
How would the compiler know how to index into the two dimensional array without specifying the second dimension?

Note your comments here:
1
2
3
4
// Position c, 0 is the remainder of c value divided by 13 (# of faces)
card[c][0] = c % 13;
// Position c, 1 is the division of c value divided by 13 (# of faces)
card[c][1] = c / 13;


I don't know if you've learned structs yet, but there is a better way to do this:
1
2
3
4
5
6
7
8
struct Card
{  int suit;
    int face; 
};
Card cards[52];
...
cards[c].suit = c % 4;
cards[c].face = c % 13;


Last edited on
Thank you very much for your reply, the code button wasn't working and I couldn't remember what to type for it... I fixed it though :)

I get what you're saying, but would that mean if I had short card [][][] I would need to put a 3 in the third set of brackets and I could leave the other 2 blank?

Edit:

Thanks for the second part, we didn't get into that yet! But I will look into that myself!
Last edited on
would that mean if I had short card [][][] I would need to put a 3 in the third set of brackets and I could leave the other 2 blank?

No. The [2] does not indicate the number of dimensions. It indicates the size of the dimension.

If you had a three dimensional array, you would need to specify the size of both the second and third dimensions. Only the first dimension can be omitted.
Last edited on
Ooooh, awesome. Thanks a ton!
Topic archived. No new replies allowed.