I am having a compiling issue because of some syntax errors while trying to return an array. You can probably ignore most of the code below; I am only concerned with the call and return of the CreateBoard[][] array. As I understand a pointer might be necessary to do this. Does anyone have some tips that I can use?

int main()
{
    int Board[9][9];
    Board = CreateBoard();   
    int difficulty = Difficulty();
    WriteFile(Board);
    Display(Board);
    system ("pause");
    return 0;
}
  
int CreateBoard()
{
    int row = 0;
    int column = 0;
    bool BoardStatus [9][9];  
    int Board [9][9];  // Creates the Sudoku board
    int BoardTemp;
    Reset(Board, BoardStatus);
    for (row = 0; row <= 8; row++) 
    {
        for (column = 0; column <= 8; column++)
        {
            while (BoardStatus [row][column] == false)
            {
               if (Board [row][column] == -1)
               {
                 Board [row][column] = Random_Number();  // a random number is placed inside every cell before the validation process begins in order to randomize each puzzle.
                 BoardTemp = Board [row][column];
               }
               BoardStatus [row][column] = Verify(Board, row, column);
               if (BoardStatus [row][column] == false)
               {
                     Board [row][column] = Board[row][column]+ 1;
                     if (Board [row][column] == 10) // A value inside a cell cannot be greater than 9.
                     {
                          Board[row][column] = 1;
                     }
                     if (BoardTemp == Board[row][column] )
                     {
                          Reset(Board, BoardStatus);
                          row = 0;
                          column = 0;     
                     }
               }            
            }
        }
    }
    return Board;
}

Recommended Answers

All 5 Replies

why not just create the array in main() and pass it as a parameter

int CreateBoard(int Board[9][9])
{
   // all your code here
    return 0;
}

int main()
{
    int Board[9][9];
    CreateBoard(Board);   
    return 0;
}

> Does anyone have some tips that I can use?
C++ doesn't allow you to return an array. You can return a pointer to an array or a pointer to the first element of an array. Both of those are tricky to get right if the array is local to the function returning it. The best all around trick using arrays is to pass the array as a parameter like Ancient Dragon showed. The best solution is probably to use the STL vector instead of an array. With a vector you can return a copy of the vector and all of the problems with returning arrays go away. :)

another vote for stl vectors!!

hmm. I just got everything up and running using AD's original suggestion with Board[9][9] defined in main(). Perhaps, after I understand vectors a little more clearly, I will replace the arrays with them. Thanks for the help

vectors are great for single dimension arrays. As you add more dimensions vectors become much more complicated than simple int arrays like Board. Here's an example of a 2d array of vectors

typedef vector<int> COLUMNS;
vector<COLUMNS> Board;

//or you can do this too
vector< vector<int> > Board;

The nice thing about the above is that the rows do not have to all contain the same number of columns, that is the number of columns can be different for each row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.