Q: Output some values in special format I'm trying to solve some problems. but there is something wrong with my code.
The problem is : Given a number n, output n*n in the following format.
Input:
n=3

Output:
 7 8 1 
 6 9 2 
 5 4 3

Here is my code: (I know there are some bugs in it, but I can't fix it. If you have better ideas, help me.)
 Thank in advance.
#include<stdio.h>
#include<stdlib.h>
void oput(int **matrix,int n);
int main()
{   int x=0,y=0,n,k=1;
    scanf("%d",&n);
    int **matrix = malloc(sizeof(int)*n*n);
    if(matrix==NULL)
    {
        perror("can't allocate");
        return 1;
    }
    //Input values to x*y matrix
   while(n>=1)
    {
      for(x=0,y=n-1;x<n;x++)
      {
          matrix[x][y]=k++;
      }
      for(x=n-1,y=n-1;y>=0;y--)
      {
          matrix[x][y]=k++;
      }
      for(x=n-1,y=0;x>=0;x--)
      {
          matrix[x][y]=k++;
      }
      n--;
      }
      oput(matrix,n);
      free(matrix);
      return 0;
}
//output values by row.
void oput(int **matrix,int n)
{   
    int i=0,j=0;
    for(i=0;i<n;i++)
    {
       for(j=0;j<n;j++)
       {
          printf("%d ",matrix[i][j]);
       }
       printf("\n");
    }
}


Error arises at running time.

A: Just for fun I worked on a solution for this problem.
I'm not allowed to write comments under the question, yet, so I assumed that starting the "spiral numbering" from the upper right corner of the matrix was a requirement of the problem.
My solution consists in adding numbers all over the perimeter, using four different loops. And then starting again adding an indentation level.
#include <stdio.h>
#include <string.h>

void printMatrix( int *nMatrix, int n, char *title )
{
  int i, j;

  if( title )
    printf( "%s\n", title );

  for( i=0; i<n; i++ )
  {
    for( j=0; j<n; j++ )
    {
      printf( "%d\t", *(nMatrix+n*i+j) );
    }

    printf( "\n" );
  }
}

void main( int argc, char *argv[] )
{
  if( argc == 2 )
  {
    int n = atoi ( argv[1] );
    int i, j;

    printf( "Input value: %d\n", n );

    if( n > 0 && n <= 10 )
    {
      int spiral[n][n];
      int indentNr, x, y, lastNr=0;

      memset( spiral, 0, n*n*sizeof( int ));  

      /* As indentNr is incremented, inner frames are filled */
      for( indentNr=0; n-2*indentNr>0; indentNr++ )
      {
        /* Go South */
        for( x=indentNr, y=n-1-indentNr; x<n-indentNr; x++ )
        {
          spiral[x][y] = ++lastNr;
        }

        /* Go West (Village People, 1979) */
        for( y=n-2-indentNr, x=n-1-indentNr; y>=indentNr; y-- )
        {
          spiral[x][y] = ++lastNr;
        }

        /* Go North */
        for( x=n-2-indentNr, y=indentNr; x>=indentNr; x-- )
        {
          spiral[x][y] = ++lastNr;
        }

        /* Go East */
        for( y=1+indentNr, x=indentNr; y<n-1-indentNr; y++ )
        {
          spiral[x][y] = ++lastNr;
        }
      }

      /* Print results */
      printMatrix( &(spiral[0][0]), n, "Final spiral:" );

    }
    else
    {
      printf( "ERROR: 'n' too big!\n" );
    }
  }  
  else
  {
    printf( "USAGE: spiral n\n" );
  }
}

Please note that:


*

*Input of 'n' is inserted with main argument (no scanf)

*I limited 'n' to 10

*This solution is iterative. I did not analyse recursive solutions

*There is probably a more elegant solution for skipping all 4 loops for inner frames with n=1 and n=2. I did not spent much time in figuring it out

*I tested both odd and even scenarios and both seem to work fine. I tested up to n=10. I don't expect troubles for bigger numbers (except for presentation issues in case of big numbers), but feel free to report me any bug.

