0

I am creating a deck of bingo cards. The deck knows the amount of cards in the deck, the size of the cards, and the max number to appear on the card.

I want to create n arrays (cards) of unique random integers and then add each of these arrays to each element of another array (the deck). So, an array of arrays.

Ex.

card1 = {1, 2, 5};
card2 = {3, 7, 2};
deck = {card1, card2};

Here is my code so far:

Deck::Deck(int cardSize, int cardCount, int numberMax){
    int randInt;
    int size = cardSize * cardSize;
    int deckArr[cardCount];
    for(int t = 0; t < cardCount; t++){
        int arr[size];
        for(int i = 0; i < size; i++){
            randInt = computeRandInt(numberMax, cardSize);
            if(arr[i] == 0) arr[i] = randInt;
            else if (randInt != arr[i-1]) arr[i] = randInt;
        }
        deckArr[t] = arr;
    }
}

I get the error invalid conversion from ‘int*’ to ‘int’. How do I declare my deck array to store these card arrays?

1
  • int deckArr[cardCount] -- int arr[size]; -- This is not valid C++. An array in C++ must be declared using a constant to denote the number of entries, not a variable. Use std::vector<int> instead. Commented Nov 19, 2017 at 23:19

1 Answer 1

1

In C++, you can deal with arrays of arrays by means of nested vectors, e.g. a vector<vector<int>> for your deckArr. Note that deckArr should actually be a member of Deck (and not a local variable); Just slightly adapted your code to show the idea of nested vectors:

Deck::Deck(int cardSize, int cardCount, int numberMax){
    int randInt;
    int size = cardSize * cardSize;
    vector<vector<int>> deckArr(cardCount);
    for(int t = 0; t < cardCount; t++){
        vector<int> arr(size);
        for(int i = 0; i < size; i++){
            randInt = computeRandInt(numberMax, cardSize);
            if(arr[i] == 0) arr[i] = randInt;
            else if (randInt != arr[i-1]) arr[i] = randInt;
        }
        deckArr.push_back(arr);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. So how would I access the deck vector in another print function to print out each card?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.