0

I am very new to C++, so probably there are lots of different approaches to the problem that i want to solve, feel free to share your opinions and comments please.

void printCards(int cardCount)
{
    std::array<int, 15> cardArray{};

    std::array <std::array<int, 3>, 9> preparedCardArray{};

    std::cout << '\n';

    for (int i = 0;i < cardCount; i++)
    {
        cardArray = createCard();

        cardArray = sortCard(cardArray);

        preparedCardArray = prepareCardsForBingo(cardArray);

        cardToConsole(preparedCardArray);
        
        std::cout << '\n';
    }
}

In the code above i create an std::array which has a type

std::array <std::array<int, 3>, 9>

so when i create that std::array in runtime and store it, i can update that std::array later, it is okay when cardCount = 1. I want know how to store std::arrays when card cardCount > 1 count increases and all of the arrays needs to update in runtime.

enter image description here

-update I am creating a console app. It is a bingo app after some numbers between 1 - 90 randomly picked. I need to remove those numbers from arrays and update them in console. So i need to reach those arrays that created before.

UPinar
  • 1,067
  • 1
  • 2
  • 16
  • Please read and apply ["What is the proper way to approach Stack Overflow as someone totally new to programming?"](https://meta.stackoverflow.com/q/254572/11107541) As long as you do that, [you don't need to tell us that you are new to `X`](https://meta.stackoverflow.com/q/296391/11107541). Also, we don't do opinionated answers here. See [/hetp/dont-ask](https://stackoverflow.com/help/dont-ask). – starball Jan 03 '23 at 10:17
  • What is numberCountInCard, columnCountCard, rowCountCard? What is the definition of createCard? – kiner_shah Jan 03 '23 at 10:20
  • 1
    It isn't really clear what the problem is. Can you post a [mcve]? – n. m. could be an AI Jan 03 '23 at 10:21
  • @kiner_shah they all integers – UPinar Jan 03 '23 at 10:24
  • Probably what you need is `std::vector` its like `std::array`, but with dynamic size. – sklott Jan 03 '23 at 10:25
  • If you want a function to effect an existing object, then you need to pass the object into the function as a parameter (probably by reference), not declare it inside the function. – john Jan 03 '23 at 10:26
  • @sklott okay i create 2 vectors then how can i call them in runtime what are their names ? – UPinar Jan 03 '23 at 10:26
  • Same way you do with `std::array` the only difference that for `std::vector` you can change size in run-time, i.e. you can add elements when needed. – sklott Jan 03 '23 at 10:28
  • @UPinar The vector names are whatever you used when you declared them, same as any other variable. However my feeling is that vectors are not the answer you are looking for. Still not completely sure what the problem is however. – john Jan 03 '23 at 10:29
  • @sklott in this example when i create 2 arrays, how can i call them and update them in other function? – UPinar Jan 03 '23 at 10:29
  • @UPinar Like I said, you must pass them into the function as parameters, not declare them in the function. – john Jan 03 '23 at 10:30
  • `sortCard` is a misleading choice of function name, at least for me. I'd expect a function with this name to have signature `void sortCard(std::array& arr)` instead of returning a value. If you return a value the function name doesn't tell you anything about whether an argument passed remains unmodified or not.. – fabian Jan 03 '23 at 10:31
  • @UPinar Or alternatively you must pass them as parameter to that other function. – john Jan 03 '23 at 10:31
  • @UPinar Basically the solution it this. You have a variable in one function, and you want to access or update that variable in another function. The answer is to use function parameters to pass the variable (or a reference to it) from the first function to the second function. – john Jan 03 '23 at 10:32
  • @UPinar So based on the update, I think you have declared your arrays in the wrong place. Because the arrays are declared in the function `printCards` they are also destroyed in the function `printCards` and that is not what you want. Instead you must declare the arrays in another function (`main` presumably) and (again) use parameters to pass the arrays to your `printCards` function. – john Jan 03 '23 at 10:37
  • @john so how to declare arrays dynamically in main ? what happens for example user wants 3 cards in runtime ? – UPinar Jan 03 '23 at 10:42
  • @UPinar So each 'card' is an 2D array? Then you need a vector of 2D arrays `std::vector, 9>>` Sklott in the answer below seems to be on the right track. – john Jan 03 '23 at 10:45
  • yes each card is array but they have a type of `std::array , 9>` so i think i need `std::vector, 9>>` is it possible ? – UPinar Jan 03 '23 at 10:47
  • @UPinar Yes it's possible, it's perfectly normal. But use `using ...` declarations to make it a bit more understandable, again see sklott's answer. – john Jan 03 '23 at 10:49
  • @john first each card have type of `std::array;` then after some steps they both become `std::array , 9>` and then printed. Okay i understand the using too.. – UPinar Jan 03 '23 at 10:50
  • @fabian i understand what did you mean maybe `sortedCard` will be better. Thank you for advice. – UPinar Jan 03 '23 at 10:55

2 Answers2

2

I think that what you meant to do is something like this:

using CardType = std::array<int, 15>;
using PreparedCardType = std::array <std::array<int, 3>, 9>;

std::vector<CardType> createCards(int cardCount)
{
    std::vector<CardType> result;
    for (int i = 0;i < cardCount; i++) {
        result.push_back(createCard());
    }
    return result;
}

std::vector<PreparedCardType> prepareCards(const std::vector<CardType>& cards) {
    std::vector<PreparedCardType> result;
    std::transform(cards.begin(), cards.end(), std::back_inserter(result), prepareCardsForBingo);
    return result;
}

void void printCards(const std::vector<PreparedCardType>& prepCards)
{
    for (auto& card: prepCards) {
        cardToConsole(preparedCardArray);
        std::cout << '\n';
    }
}

void some_func(int cardCount)
{
    auto cards = createCards(cardCount);
    auto prepCards = prepareCards(cards);
    printCards(prepCards);
}
sklott
  • 2,634
  • 6
  • 17
1

You need some sort of container to store your bingo card decks.

You can try something like this:

// At global scope (where you declared your functions)

using BingoCard = std::array<std::array<int, 3>, 9>;

// bingo cards generator
std::vector<BingoCard> createBingoCards(int cardCount);

// outputs cards to console
void printCards(const std::vector<BingoCard>& cards);

// outputs a single card to console.
void cardToConsole(const BingoCard& card);

// In a cpp file, define the generator

std::vector<BingoCard> createBingoCards(int cardCount)
{
    std::vector<BingoCard> result;
    result.reserve(cardCount);      // pre-allocate space for cards.

    for (int i = 0; i < cardCount; ++i)
    {
        auto cardArray = sortCard(createCard());
        result.push_back(prepareCardsForBingo(cardArray));
    }
    return result;
}

// and the print function. 

void printCards(const std::vector<BingoCard>& cards)
{
    std::cout << '\n';
    for (auto& card : cards)
    {
        cardToConsole(card);
        std::cout << '\n';
    }
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • Thank you for your answer. I just went [Range based loop: get item by value or reference to const?](https://stackoverflow.com/a/15176127/17755777) from your answer. – UPinar Jan 03 '23 at 16:19
  • In this case, the const keyword is redundant, since the parameter `cards` is itself const, the loop reference `card` into `cards` can only be const. Not adding a redundant const keyword to the loop makes for less clutter. :) – Michaël Roy Jan 03 '23 at 17:15
  • When in doubt, since you're learning, you could replace `auto&` with `BingoCard&` and compile. The compiler should complain about not being able to convert from `const BingoCard&` to `BingoCard&`. – Michaël Roy Jan 03 '23 at 17:19
  • Yes i did try that. – UPinar Jan 03 '23 at 18:08