2d array question

This is a discussion on 2d array question within the C++ Programming forums, part of the General Programming Boards category; i am currently doing this project: Link i should use a recursive array function, and was thinking about it, could ...

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    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!

  2. #2
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    Last edited by samGwilliam; 04-07-2005 at 11:42 PM.
    MSVC++ 6.0

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    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);
          }
       }
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > 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;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    ah lol ...stupid mistakes my bad. thanks for your help.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    well i start the array on 1 because there are coordinates read from a file, which none contain row or col 0, so i had to start on 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  2. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21