# How to write Spiral matrix program?

#### Abhijit Singh

• Apr 27th, 2007

/*************  Creates a spiral matrix square or rectangular .************/

/* It first finds all the indices where items are to be placed. After one iteration of
the 4 loops, matrix size is reduced. "Start" variable points to the lcation from
where the filling has to start
*/

typedef struct
{
int m,n;
}Index;

int Create_Spiral_Matrix ( int Linear[],int Size )
{
int **Spiral;
int m = 0 ,n = 0, p = m , q = n, i;
int c = 0, Start = 0;
Index *I;

while ( m*n != Size )
{
printf("nFeed in the dimensions of the Spiral Matrix to be formed := ");
scanf("%d %d",&m,&n);
}
p = m;   q = n;

Spiral = (int **) malloc( m * sizeof(int *));   //Check Mem Allocation To Prevent Crash.
for ( i = 0; i < m; i++)
Spiral[i] = (int *) malloc( m * sizeof(int));     //Check Mem Allocation To Prevent Crash.

I = (Index *) malloc(sizeof(Index) * m * n);   //Check Mem Allocation To Prevent Crash.

while ( c < Size  )
{
for ( i = Start; i < n; i++,c++)
{
I[c].m = Start;
I[c].n = i;
}
for ( i = Start+1; i < m; i++,c++)
{
I[c].m  = i;
I[c].n = n-1;
}
for ( i = n-2; i >= Start; i--,c++)
{
I[c].m = m-1;
I[c].n = i;
}
for ( i = m-2; i >= Start+1; i--,c++)
{
I[c].m = i;
I[c].n = Start;
}

m --;  n --;
Start++;

if ( m-1 == Start )
{
if ( m > n )
for ( i = Start; i < n; i++,c++)
{
I[c].m = Start;
I[c].n = n - Start;
}
if ( n < m )
for ( i = Start; i < m; i++,c++)
{
I[c].m = Start;
I[c].n = n - Start;
}
}
}

i = 0;
for (  m = 0; m < p ; m++,printf("n"))
for (  n = 0; n < q ; n++)
Spiral [I[i].m] [I[i].n] = Linear[i++];

for (  m = 0; m < p ; m++,printf("n"))
for (  n = 0; n < q ; n++)
printf("%dt",Spiral[m][n]);
return 0;
}

int main()
{
int Linear1[] = { 1, 5, 8, 9, 5, 7, 4, 8, 0, 2, 3, 6,
4, 1, 7, 2, 5, 9, 3, 2, 5, 6, 1, 2,
5, 7, 3, 8, 5, 3, 6, 1, 2, 4, 9, };

int Linear2[] = { 1, 5, 8, 5, 7, 8, 0, 6, 2,
4, 1, 7, 2, 9, 3, 1, 2, 9,
5, 7, 3, 8, 5,  2, 4,  };

Create_Spiral_Matrix(Linear1, sizeof(Linear1)/4);
Create_Spiral_Matrix(Linear2, sizeof(Linear2)/4);
printf("n");
return 0;
}

#### Abhijit Singh Profile Answers by Abhijit Singh

• Apr 27th, 2007

/*~~~~~~~~~ Creates square and  rectangular matrices ~~~~~~~~*/
/* Input required is dim of the spiral matrix. Just feed in two factors of the size of array. i.e If Items are 35 then m = 5 n = 7 or m = 7 n = 5 or m = 35 n = 1 or m = 1 n = 35.  If Items are 36 then m = 1/3/4/6/9/12/36  n = 36/12/9/6/4/3/1
*/

typedef struct
{
int m,n;
}Index;

int Create_Spiral_Matrix ( int Linear[],int Size )
{
int **Spiral;
int m = 0 ,n = 0, p = m , q = n, i;
int c = 0, Start = 0;
Index *I;

while ( m*n != Size )
{
printf("nFeed in the dimensions of the Spiral Matrix to be formed := ");
scanf("%d %d",&m,&n);
}
p = m;   q = n;

Spiral = (int **) malloc( m * sizeof(int *));   //Check Mem Allocation To Prevent Crash.
for ( i = 0; i < m; i++)
Spiral[i] = (int *) malloc( n * sizeof(int));     //Check Mem Allocation To Prevent Crash.

I = (Index *) malloc(sizeof(Index) * m * n);   //Check Mem Allocation To Prevent Crash.

while ( c < Size  )
{
for ( i = Start; i < n; i++,c++)
{
I[c].m = Start;
I[c].n = i;
}
for ( i = Start+1; i < m; i++,c++)
{
I[c].m  = i;
I[c].n = n-1;
}
for ( i = n-2; i >= Start; i--,c++)
{
I[c].m = m-1;
I[c].n = i;
}
for ( i = m-2; i >= Start+1; i--,c++)
{
I[c].m = i;
I[c].n = Start;
}

m --;  n --;
Start++;

if ( m-1 == Start )
{
if ( m > n )
for ( i = Start; i < n; i++,c++)
{
I[c].m = Start;
I[c].n = n - Start;
}
if ( n < m )
for ( i = Start; i < m; i++,c++)
{
I[c].m = Start;
I[c].n = n - Start;
}
}
}

i = 0;
for (  m = 0; m < p ; m++,printf("n"))
for (  n = 0; n < q ; n++)
Spiral [I[i].m] [I[i].n] = Linear[i++];

for (  m = 0; m < p ; m++,printf("n"))
for (  n = 0; n < q ; n++)
printf("%dt",Spiral[m][n]);
return 0;
}

int main()
{
int Linear1[] = { 1, 5, 8, 9, 5, 7, 4, 8, 0, 2, 3, 6,
4, 1, 7, 2, 5, 9, 3, 2, 5, 6, 1, 2,
5, 7, 3, 8, 5, 3, 6, 1, 2, 4, 9 };

int Linear2[] = { 1, 5, 8, 5, 7, 8, 0, 6, 2,
4, 1, 7, 2, 9, 3, 1, 2, 9,
5, 7, 3, 8, 5,  2, 4  };

Create_Spiral_Matrix(Linear1, sizeof(Linear1)/4);
Create_Spiral_Matrix(Linear2, sizeof(Linear2)/4);
printf("n");
return 0;
}

#### ashoknaina Profile Answers by ashoknaina

• Dec 20th, 2011

```Code#include<stdio.h>
main()
{
int a,i,j,n,m,p,q,k=0;
printf("
Enter Order Of matrix");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);

p=m;
q=n;
i=j=1;

while(k<p*q)
{
for(;j<=n&&k<p*q;j++)
{
printf("%d ",a[i][j]);
k++;
}
j--;
i++;
for(;i<=m && k<p*q;i++)
{
printf("%d ",a[i][j]);
k++;
}
i--;
j--;
for(;j>=i-m+1 && k<p*q;j--)
{
printf("%d ",a[i][j]);
k++;
}
j++;
i--;
for(;i>1 && k<p*q;i--)
{
printf("%d ",a[i][j]);
k++;
}
if(k<p*q)
{
j++;
i++;
n--;
m--;
}
}
}```  