Something I'm misunderstanding about rand() % i? : <pre><code>int main() {
const int n = 5;
int A[n][n]; // value of each cell
int V[n][n]; // total value of each cell

for (int i = 0; i &lt; n; i++)
{
    for (int j = 0; j &lt; n; j++)
    {
        V[i][j] = 0; // initialize total value of each cell equal to zero
        A[i][j] = rand() % 10; // set each cell's value equal to some number 0-9
        printf("%i ", A[i][j]);
    }
    printf("\n");
}

for (int i = 0; i &lt; n; i++)
{
    for (int j = 0; j &lt; n; j++)
    {
        if (i == 0 &amp;&amp; j == 0)
        {
            V[i][j] = A[i][j];
        }
        else if (i == 0)
        {
            V[i][j] = V[i][j - 1] + A[i][j];
        }
        else if (j == 0)
        {
            V[i][j] = V[i - 1][j] + A[i][j];
        }
        else
        {
            if (V[i][j - 1] &gt; V[i - 1][j])
            {
                V[i][j] = V[i][j - 1] + A[i][j];
            }
            else
            {
                V[i][j] = V[i - 1][j] + A[i][j];
            }
        }
    }
}
printf("\n");
for (int i = 0; i &lt; n; i++)
{
    for (int j = 0; j &lt; n; j++)
    {
        if (V[i][j] &lt; 10) printf(" %i ", V[i][j]);
        else printf("%i ", V[i][j]);
    }
    printf("\n");
}

cin.get();
</code></pre>

<p>}</p>

<p>This outputs <a href="http://i.imgur.com/Ak3KpPr.png" rel="nofollow">http://i.imgur.com/Ak3KpPr.png</a></p>

<p>What I don't understand is why V[0][2] outputs 12 when it should output V[0][1] + A[0][2], or 7+4.</p>

<p>Context:
At a garage sale one day, you stumble upon an old school video game. In this video game, your
character must take a journey along an n × n grid, collecting rewards along the way. Specifically,
there is an n × n matrix A with nonnegative entries, and your character collects a reward equal to
Aij if he visits the cell (i, j) of the grid. Your objective is to maximize the sum of rewards collected
by your character.
(a) [4 points]. The rules of level one of the game are as follows. Your character starts at the
top-left corner — i.e., cell (1, 1) — of the grid, and must travel to the the bottom-right corner —
i.e., cell (n, n) — in sequence of steps. At each step, your character is allowed to move either one
cell to the right or one cell down in the grid; stepping upwards, to the left, or diagonally is not
2
allowed. Show how to compute the optimal journey in O(n
2
) time.</p>
