0% found this document useful (0 votes)
59 views4 pages

In Spiral Order.: Spiral Matrix This Program Prints The Numbers in Given Array (Row-Major Order)

This program prints the numbers in a 2D array in spiral order. It takes in the dimensions of the array as input and prints out the elements in spiral fashion, starting from the top left corner and moving outwards. The code uses a while loop with nested if conditions to traverse the array in clockwise spiral order, printing each element.

Uploaded by

vaithika
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views4 pages

In Spiral Order.: Spiral Matrix This Program Prints The Numbers in Given Array (Row-Major Order)

This program prints the numbers in a 2D array in spiral order. It takes in the dimensions of the array as input and prints out the elements in spiral fashion, starting from the top left corner and moving outwards. The code uses a while loop with nested if conditions to traverse the array in clockwise spiral order, printing each element.

Uploaded by

vaithika
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SPIRAL MATRIX

This Program prints the numbers in given array(Row-Major Order)


in Spiral order.

Ex: Order Is 3*4

11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26

Spiral Order Of Matrix is 11 12 13 14 18 22 26 25 24 23 19 15 16 17 21 20


C Code Snippet (Toggle Plain Text)

#include<stdio.h>
main()
{
int a[20][20],i,j,n,m,p,q,k=0;
printf("\n 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--;
}
}
}

Or

#include //stdio.h
#include //conio.h
#include //stdlib.h
#define maxel 10

void main()
{
int a[maxel][maxel] = {0},i,j,m,n,t=0,cnt=0;
clrscr();
printf("\nEnter the order of the matrix:");
scanf("%d%d",&m,&n);
printf("\nEnter the matrix elements:");

for(i=0;i
{
for(j=0;j
{
scanf("%d",&a[i][j]);
}
}
clrscr();
printf("\nThe given matrix is:\n\n");
for(i=0;i
{
for(j=0;j
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n\n");
getch();
i=j=t=0;
cnt=0;
while(m>0||n>0)
{
for(j=t;cnt!=n;j++)
{
printf(" %d ",a[i][j]);
cnt++;
}
m--;
j--;
if(m==0)
break;
cnt=0;
for(i++;cnt!=m;i++)
{
printf(" %d ",a[i][j]);
cnt++;
}
n--;
i--;
if(n==0)
break;
cnt=0;
for(j--;cnt!=n;j--)
{
printf(" %d ",a[i][j]);
cnt++;
}
m--;
j++;
if(m==0)
break;
cnt=0;
for(i--;cnt!=m;i--)
{
printf(" %d ",a[i][j]);
cnt++;
}
n--;
i++;
if(n==0)
break;
cnt=0;
t++;
}
printf("\n\nThank you...");
getch();
}

You might also like