GeekInterview.com
Series: Subject: Topic:
Question: 21 of 21

How to write Spiral matrix program?

Asked by: Interview Candidate | Asked on: Feb 6th, 2007
Showing Answers 1 - 3 of 3 Answers
Abhijit Singh

Answered On : 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;
}

  
Login to rate this answer.
Abhijit Singh

Answered On : Apr 27th, 2007

View all answers by Abhijit Singh

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


  
Login to rate this answer.
ashoknaina

Answered On : Dec 20th, 2011

View all answers by ashoknaina

Code
  1. #include<stdio.h>
  2. main()
  3. {
  4. int a[20][20],i,j,n,m,p,q,k=0;
  5. Enter Order Of matrix");
  6. scanf("%d%d",&m,&n);
  7. for(i=1;i<=m;i++)
  8. for(j=1;j<=n;j++)
  9.     scanf("%d",&a[i][j]);
  10.  
  11. p=m;
  12. q=n;
  13. i=j=1;
  14.  
  15. while(k<p*q)
  16. {
  17.     for(;j<=n&&k<p*q;j++)
  18.     {
  19.         printf("%d ",a[i][j]);
  20.         k++;
  21.     }
  22.     j--;  
  23.     i++;
  24.     for(;i<=m && k<p*q;i++)
  25.     {
  26.         printf("%d ",a[i][j]);
  27.         k++;
  28.     }
  29.     i--;
  30.     j--;    
  31.     for(;j>=i-m+1 && k<p*q;j--)
  32.     {
  33.         printf("%d ",a[i][j]);
  34.         k++;
  35.     }    
  36.     j++;
  37.     i--;
  38.     for(;i>1 && k<p*q;i--)
  39.     {
  40.         printf("%d ",a[i][j]);
  41.         k++;
  42.     }
  43.     if(k<p*q)
  44.     {
  45.         j++;
  46.         i++;
  47.         n--;
  48.         m--;
  49.     }
  50. }
  51. }

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Algorithm Questions

ColdFusion Interview Questions

Computer Awareness Questions

Delphi Interview Questions

Perl Interview Questions

PHP Interview Questions

Python Interview Questions

VBA Interview Questions

VoiceXML Interview Questions

XML Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.