title: Creating outward spiral
body: <p>I've been thinking about this and I just can't think of a way to fill in a matrix with an outward spiral, so that I can do the following:</p>
  
  <p>Turn this:
  1 2 3 4 5 ... n</p>
  
  <p>To</p>
  
  <pre><code>21 22 23 24 25 26
  20 07 08 09 10 27
  19 06 01 02 11 28
  18 05 04 03 12 29
  17 16 15 14 13 30
             ...n
  </code></pre>
  
  <p>My problem is the algorithm itself, but if instead of pseudocode you can help with C++, that'd be better.</p>
  
  <p>This is some code I wrote to test things out, but I really don't know how I can go about to do this.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main() {
    //int n = 5;
    int spiral[5][6];
  
    for (int i = 0; i &lt; 5; i++)
      for (int u = 0; u &lt; 6; u++)
        spiral[i][u] = 0;
  
    spiral[2][2] = 1;
    string direction = "right";
    for (int i = 2; i &lt; 5; i++) {
      for (int u = 2; u &lt; 6; u++) {
        if (direction == "right") {
          spiral[i][u + 1] = spiral[i][u] + 1;
          direction = "down";
        }
      }
    }
  
    for (int i = 0; i &lt; 5; i++) {
      for (int u = 0; u &lt; 6; u++) {
        printf("%02d ", spiral[i][u]);
      }
      printf("\n");
    }
  
    return 0;
  }
  </code></pre>
  
  <p>Thank you!</p>