That did the trick exactly. Thanks.
Now that I figured that wasn't my problem, I've come to it and it has to do with this recursive function I wrote. Imagine two grids, filled with 0s and 1s, and these two grids are identical in content, but not addresses. I iterate through each position in the grid and compare if they are the same. If they are the same, then I call the recursive function. This function then changes the value from 1 to -1 to indicate it has been there. It then checks to see if it can go NSEW (north south east west) by seeing if there are other 1's in those positions, and marks them. I am getting an error when traversing through this grid. I think it has something to do with my bounds checking.
Code:
void Grid::markRegions( int r, int c ){
if( markedRecords[r][c] != -1 ) //if cell is not marked
{
mark(r, c);
if( c+1 < vals[c].size() && vals[r][c+1] == 1 ) //go east
{
markRegions(r, c+1);
}
if( r+1 < vals[r].size() && vals[r+1][c] == 1 ) //go south
{
markRegions(r+1, c);
}
if( r-1 != -1 && vals[r-1][c] == 1 ) //go north
{
markRegions(r-1, c);
}
if( c-1 != -1 && vals[r][c-1] == 1 ) //go west
{
markRegions(r, c-1);
}
} //end of top if
}
Code:
void Grid::mark(int r, int c)
{
markedRecords[r][c] = -1;
}
Code:
void Grid::traverse()
{
for( int i = 0; i < nRows; i++ )
{
for( int j = 0; j < nCols; j++ )
{
if( vals[i][j] == 1 && markedRecords[i][j] == 1 )
{
markRegions(i, j);
}
}
}
}
My traverse function calls the recursive function. I have posted the code so you know exactly what each function does instead of "guessing".