title: how to build spiral square matrix using recursion?
body: <p>I wanted to build a spiral square matrix using recursion. I am able to build spiral square matrix using iterative method as below:</p>
  
  <pre><code>void main() 
  { 
  
      int initial_direction = UP , n = MAX , p = 1 ;    /* intial_direction 
  
      is set to UP because we need to start moving right */
  
      int r ,c , a[MAX][MAX]; 
  
      int row_right  = 0 , column_down = n-1 , row_left = n-1 , column_up = 0 ; 
  
      clrscr (); 
  
      //Set all elements of the matrix to 0 
  
      for(r = 0 ; r &lt; MAX ; r++) 
      { 
          for(c = 0 ; c &lt; MAX ; c++) 
              a[r][c] = 0 ; 
  
      } 
  
  
      //Generate elements of the spiral matrix 
  
      while(p != n*n+1) 
      { 
  
            if(initial_direction == UP) 
            { 
              //Move RIGHT 
  
              r = row_right++ ; 
  
              for(c = 0 ; c &lt; n ; c++) 
              { 
                  if(a[r][c] == 0) 
                      a[r][c] = p++; 
  
              } 
  
  
  
  
              initial_direction = RIGHT ; 
            } 
            else if(initial_direction == RIGHT) 
            { 
              //Move down 
  
              c = column_down-- ; 
  
              for(r = 0 ; r &lt; n ; r++) 
              { 
  
                  if(a[r][c] == 0) 
                      a[r][c] = p++; 
              } 
  
  
              initial_direction = DOWN ; 
  
  
  
  
            } 
            else if(initial_direction == DOWN) 
            { 
              //Move left 
  
              r = row_left-- ; 
  
              for(c = n-1 ; c &gt;= 0 ; c--) 
              { 
                  if(a[r][c] == 0) 
                      a[r][c] = p++; 
  
              } 
  
              initial_direction = LEFT ; 
  
  
  
  
  
            } 
            else if(initial_direction == LEFT) 
            { 
              //Move up 
  
              c = column_up++; 
  
              for(r = n-1 ; r &gt;= 0 ; r--) 
              { 
  
                  if(a[r][c] == 0) 
                    a[r][c] = p++; 
  
              } 
  
  
              initial_direction = UP ; 
            } 
  
      } 
  
  
      //Print the matrix 
  
      printf("\n\n"); 
  
      for(r = 0 ; r &lt; MAX ; r++) 
      { 
            for(c = 0 ; c &lt; MAX ; c++) 
            printf("%4d ",a[r][c]); 
  
            printf("\n"); 
  
  
  
      } 
  
  }
  </code></pre>
  
  <p>I wanted to create the same matrix using recursion:
  Here is the code i used:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  bool s[5][5] = {0};
  int counter;
  typedef enum {
      right = 0,
      down,
      left,
      up
  }c_flag;
  
  c_flag flag;
  int last;
  int build_matrix(int a[5][5],int i,int j, c_flag flag)
  {
      int ret;
      if (i &lt; 0 || i&gt;=5 || j &lt; 0 || j &gt;= 5)
      {
          if (last == right)
          {
              flag = down;
              last = flag;
          }
          if (last == down)
          {
              flag = left;
              last = flag;
          }
          if (last == left)
          {
              flag = up;
              last = flag;
          }
          if (last == up)
          {
              flag = left;
              last = flag;
          }
          return false;
      }
      if (s[i][j] == true)
      {
          if (last == right)
          {
              flag = down;
              last = flag;
          }
          if (last == down)
          {
              flag = left;
              last = flag;
          }
          if (last == left)
          {
              flag = up;
              last = flag;
          }
          if (last == up)
          {
              flag = left;
              last = flag;
          }
          return false;
      }
      if(s[i][j] == false)
      {
          s[i][j] = true;
          a[i][j] = ++ counter;
      }
      if (flag == right)
      {
          ret = build_matrix(a,i,j+1,right);
          //if (!ret)
          //  return false;
      }
      flag = down;
      last = flag;
      if (flag == down)
      {
          ret =build_matrix(a,i+1,j,down);
          //if (!ret)
          //  return false;
      }
      flag = left;
      last = flag;
      if (flag == left)
      {
          ret = build_matrix(a,i,j-1,left);
          //if (!ret)
          //  return false;
      }
      flag = up;
      last = flag;
      if (flag == up)
      {
          ret = build_matrix (a,i-1,j,up);
          //if (!ret)
          //  return false;
      }
      flag = right;
      last = flag;
      return false;
  }
  
  int main()
  {
      int i, j, n = 5;
      int k, ret;
      //printf("Enter N to construct square matrix \n");
      //scanf("%d",&amp;n);
      int a[5][5] = {0};
      k = n/2 + n%2;
      for (i = 0; i &lt; k; i++)
      {
          ret = build_matrix(a,i,i,right);
      }
      for(i = 0; i &lt; n; i++)
      {
          for(j = 0; j &lt; n; j++)
              printf("%d",a[i][j]);
          printf("\n");
      }
      return 0;
  }
  </code></pre>
  
  <p>I am getting out put for above as :</p>
  
  <pre><code>1 2 3 4 5
  
  16 19 22 25 6
  
  15 18 21 24 7
  
  14 17 20 23 8
  
  13 12 11 10 9
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>1 2 3 4 5
  
  16 17 18 19 6
  
  15 24 25 20 7
  
  14 23 22 21 8
  
  13 12 11 10 9
  </code></pre>
  
  <p>Problem is Flag is not setting proper, i dunno inside which recursion call flag is getting disturbed.
  Please some one help implement using recursion.</p>