# 2d array question

• 04-07-2005
JoshR
2d array question
i am currently doing this project: Link

i should use a recursive array function, and was thinking about it, could someone give me some pointers on making the recursive function, should i allocate each seperate object to an array? if so how would i go about appointing coordinates to each object array, without knowing how big the array should be...hmm...

and tips would be appreciated, i dont want code or huge help, just hints or tips, thanks!
• 04-07-2005
samGwilliam
I did a similar thing when I tried to write a Minesweeper clone. What you need is a separate 2D array of the same size which contains flags saying whether or not that square has already been counted. Once the algorithm is done this has to be reset.

Pseudocode looks something like this:

Code:

```checkSquare (x, y) {   if (checked [x] [y]) or (grid [x] [y] = white)       return;   // Register square as part of an object.   checked [x] [y] = true;   checkSquare (x - 1, y - 1);   checkSquare (x, y - 1);   checkSquare (x + 1, y - 1);   checkSquare (x - 1, y);   checkSquare (x + 1, y);   checkSquare (x - 1, y + 1);   checkSquare (x, y + 1);   checkSquare (x + 1, y + 1); }```
I've left bound checking and object defining to you. One strategy could be to keep a total of checked squares and while this is less than (x * y) try again with the first un-checked black square you find. This will repeat until the array has been completely searched and broken down.
• 04-09-2005
JoshR
well i came up with this... it takes in the coordinates, row * col, then it changes all black in the object to white...does anyone see an error with this, i get a unknown software exception memory error...

Code:

```void erase (char board[ MAX ][ MAX ], int row, int col) {   if ((1 <= row <= MAX) && (1 <= col <= MAX))   {       if (board[ row ][ col ] == black)       {         board[ row ][ col ] == white;              erase (board, row - 1, col);         erase (board, row + 1, col);         erase (board, row, col - 1);         erase (board, row, col + 1);       }   } }```
• 04-09-2005
Salem
> if ((1 <= row <= MAX) && (1 <= col <= MAX))
This doesn't do what you want it to.

Try
if ((1 <= row && row <= MAX) && (1 <= col && col <= MAX))

Also, since the array is MAX in size, you should be testing <, not <= if you want valid subscripts.
if ((1 <= row && row < MAX) && (1 <= col && col < MAX))

Also, why 1, when arrays start at 0

> board[ row ][ col ] == white;
You mean assignment
board[ row ][ col ] = white;
• 04-09-2005
JoshR