/*~~~~~~~~~ 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;
}