title: Why do I get access violation run-time error?
body: <p>I want to iterate diagonally over a two dimensional array. I use a normal array with <code>[N*N]</code> size instead of using an <code>[N][N]</code> array. After that, I produce the indexes.
  This is the array I want to print (for example)</p>
  
  <pre><code>1 3 6 10
  2 5 9 13
  4 8 12 15
  7 11 14 16
  </code></pre>
  
  <p>the result should be like this:</p>
  
  <pre><code>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  </code></pre>
  
  <p>I've done with the following code:</p>
  
  <pre><code>int n, temp[2], nums[100 + 10];
  cin &gt;&gt; n;
  for (int i = 0; i &lt; n * n; i++)
      cin &gt;&gt; nums[i];
  
  temp[0] = temp[1] = 0;
  for (int i = 0, cnt = 0; i &lt; n; i++, cnt += 5) {
      temp[1] = temp[0];
      for (int j = 0; j &lt;= i; j++) {
          cout &lt;&lt; nums[temp[1]] &lt;&lt; " ";
          temp[1] -= n - 1;
      }
      temp[0] += n;
  }
  
  temp[0] -= n - 1;
  for (int i = n - 2, cnt = temp[0]; i &gt;= 0; i--, cnt -= 5) {
      temp[1] = temp[0];
      for (int j = 0; j &lt;= i; j++) {
          cout &lt;&lt; nums[temp[1]] &lt;&lt; " ";
          temp[1] -= n - 1;
      }
      temp[0] += 1;
  }
  </code></pre>
  
  <p>I think it should work, but I don't know why I get access violation run-time error.
  Thanks.</p>