Note that there are some explanatory texts on larger screens.

plurals
  1. PO(VC2010 express) 2D array misbehaving
    primarykey
    data
    text
    <p>Im using VC2010 express and am encountering an result I can't quite grasp. My code is as follows:</p> <pre><code>// #include "stdafx.h" #include &lt;iostream&gt; #include &lt;time.h&gt; using namespace std; void main() { const int gridSize = 2; int grid[gridSize][gridSize]; for(int x=1; x &lt;= gridSize; x++){ for(int y=1; y &lt;= gridSize; y++){ grid[x][y] = 0; } } for(int i=0; i &lt;= gridSize; i++){ grid[i][0] = 1; // set the horizontal line to 1's grid[0][i] = 1; // set the vertical line to 1's } int rows = 0; while(rows &lt;= gridSize){ for(int i=0; i&lt;=gridSize; i++){ cout &lt;&lt; grid[i][rows] &lt;&lt; " "; } cout &lt;&lt; "\n"; rows++; } clock_t wait; wait = clock(); while (clock() &lt;= (wait + 500000)); // Wait for 500 seconds and then continue wait=0; } </code></pre> <p>I am expecting this code to result in:</p> <ul> <li>1 1 1</li> <li>1 0 0</li> <li>1 0 0</li> </ul> <p>Instead it results in:</p> <ul> <li>1 1 1</li> <li>1 0 0</li> <li>1 1 0</li> </ul> <p>I dont understand how its possible for this code to fill grid[1][2] with 1. Any thoughts on that?</p> <p><strong>EDIT:</strong> Can't answer my own question right now.. But I've solved the lattice paths problem! :) Ended up with this code to calculate the amount of lattice paths in a grid:</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;time.h&gt; using namespace std; void main() { const int gridSize = 3; int grid[gridSize+1][gridSize+1]; for(int i=0; i &lt;= gridSize; i++){ grid[i][0] = 1; // set the horizontal line to 1's grid[0][i] = 1; // set the vertical line to 1's } for(int x=1; x &lt;= gridSize; x++){ for(int y=1; y &lt;= gridSize; y++){ grid[x][y] = grid[x-1][y] + grid[x][y-1]; } } cout &lt;&lt; "Amount of lattice paths for a " &lt;&lt; gridSize &lt;&lt; "x" &lt;&lt; gridSize &lt;&lt; " grid: " &lt;&lt; grid[gridSize][gridSize]; clock_t wait; wait = clock(); while (clock() &lt;= (wait + 500000)); // Wait for 500 seconds and then continue wait=0; } </code></pre> <p>Thanks for the quick reply :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload