# vectors and the resize function

• 02-28-2012
vectors and the resize function
Hi everyone,

I am having a problem creating vectors of the correct size.

What I am doing is, reading from a file. From this file, the first 2 ints specify rows and columns of a row x col grid. I use vector of vectors to achieve this. Now, I've written this code, and it isn't giving me the desired result. When I cout to the console, it is displaying 5 rows and 40 columns when it should be displaying a 10 x 20 grid. What am I doing wrong here?

Code:

```    int x;     ifstream input;     input.open( fileName.c_str() );     input >> nRows;     input >> nCols;         cout << "row and columns " << nRows << nCols << endl;         vals.resize(nRows);     markedRecords.resize(nRows);     //cout <<  "vals size " << vals.size() << endl;     for( int i = 0; i < nRows; i++ )     {             vals[i].resize(nCols);             markedRecords[i].resize(nCols);                 for( int j = 0; j < nCols; j++ )         {                input >> x;                    vals[i][j] = x;                markedRecords[i][j] = x;             //cout << vals [i][j] << " ";             cout << markedRecords[i][j] << " ";         }     }     input.close();```
• 02-28-2012
oogabooga
You should be couting a newline between lines 30 and 31.
• 02-28-2012
recursive use
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".
• 02-28-2012
oogabooga
Should these two
Code:

```if( c+1 < vals[c].size() && vals[r][c+1] == 1 ) //go east ... if( r+1 < vals[r].size() && vals[r+1][c] == 1 ) //go south```
be like this?
Code:

```if( c+1 < vals[r].size() && vals[r][c+1] == 1 ) //go east ... if( r+1 < vals.size() && vals[r+1][c] == 1 ) //go south```
• 02-28-2012