Set Function Parameters for a Variable, row/col, Matrix

52 Views Asked by At

So, what isn't the problem? Anyway, in attempts to solve the N Queens problem using stacks library, we were met with issues trying to set function parameters to take our matrix and manipulate it.

int main(){
        const int n = 8;

        bool board[n][n];

Here we're trying to take in the board.

bool isValid(int n, bool board[][], int row, int col)

How else could we input our matrix?

We tried using Vectors and it screwed with our calculations, ruining our progress. We are currently trying to use dynamic arrays and pointers, but we don't understand the concept to be implemented in our scope.

1

There are 1 best solutions below

0
Some programmer dude On

One problem is the argument declaration bool board[][], which is equal to bool (*board)[]. And C++ doesn't allow arrays without a size, the size needs to be specified by a compile-time constant.

It might be possible to use std::array and templates, perhaps as

template<std::size_t M, std::size_t N>
bool isValid(std::array<std::array<bool, N>, M> board, int row, int col);

where std::array<std::array<bool, N>, M> board corresponds to bool board[M][N].

Note that n is no longer needed as the size is built into the arrays themselves.

Or use a global constexpr variable initialized to the actual size, and use that for the array sizes instead of template values.