Home > Blockchain >  How to create more than one std::array dynamically in run time for updating them later
How to create more than one std::array dynamically in run time for updating them later

Time:01-03

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.

CodePudding user response:

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';
    }
}

CodePudding user response:

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);
}
  • Related