Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry to everybody who wrote "The second half should be similar"... it's not.</p> <p>Anyway, here you go:</p> <pre><code>// traverse array diagonally int c, tmp, x; for (c = N - 1; c &gt; -N; c--) { tmp = N - abs(c) - 1; x = tmp; while (x &gt;= 0) { if (c &gt;= 0) { std::cout &lt;&lt; arr[x][tmp - x] &lt;&lt; ", "; } else { std::cout &lt;&lt; arr[N - (tmp - x) - 1][(N-1)-x] &lt;&lt; ", "; } --x; } std::cout &lt;&lt; "\n"; } </code></pre> <p>Do you need this for a game or something? </p> <p>[edit] looking at this again, I think my answer wasn't very nicely written. Here's a quick run through:</p> <p>Let's pretend that N is 3. </p> <p>What we need is an iteration over coordinate-combinations that looks like this:</p> <pre><code>(0, 0) (1, 0), (0, 1) (2, 0), (1, 1), (0, 2) (2, 1), (1, 2) (2, 2) </code></pre> <p>So first some placeholders:</p> <pre><code>int c, // a counter, set by the outer loop tmp, // for intermediate results x; // the x-index into *arr* (*y* will be defined implicitly) </code></pre> <p>Now this outer loop</p> <pre><code>for (c = N - 1; c &gt; -N; c--) { </code></pre> <p>makes <em>c</em> iterate over <em>{2, 1, 0, -1, 2}</em>. </p> <p>The next step</p> <pre><code> tmp = N - abs(c) - 1; x = tmp; </code></pre> <p>turns <em>{2, 1, 0, -1, -2}</em> into <em>{0, 1, 2, 1, 0}</em>, which are the lengths of the needed outputs at this step minus one (so they can be used as indices). We make two copies of this, <em>tmp</em> and <em>x</em>.</p> <p>Now we count down from <em>x</em> to <em>0</em>:</p> <pre><code> while (x &gt;= 0) { ... --x; } </code></pre> <p>if we're on the upper-left half of <em>arr</em>, indicated by <em>c >= 0</em>, the x-indices into <em>arr</em> need to start at the diagonal and go down to zero <em>(0 to 0, 1 to 0 and 2 to 0)</em> , whereas the y-indices need to start at zero and go up to the diagonal <em>(0 to 0, 0 to 1 and 0 to 2)</em>:</p> <pre><code> if (c &gt;= 0) { std::cout &lt;&lt; arr[x][tmp - x] &lt;&lt; ", "; } </code></pre> <p>once we're on the lower-right half, the x-indices need to start at <em>N</em> and to down to the diagonal <em>(2 to 1 and 2 to 2)</em>, whereas the y-indices need to start at the diagonal and go up to <em>N (1 to 2 and 2 to 2)</em>:</p> <pre><code> else { std::cout &lt;&lt; arr[N - (tmp - x) - 1][(N-1)-x] &lt;&lt; ", "; } </code></pre> <p>finally we just need a line-break at the end of each line:</p> <pre><code> std::cout &lt;&lt; "\n"; </code></pre> <p>Savy? :-)</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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