Spiral
Matrix or Circular Matrix - Produce a spiral array. A spiral array is a square
arrangement of the first N2 natural numbers, where the numbers increase
sequentially as you go around the edges of the array spiralling inwards.
#include <stdio.h>
#include <conio.h>
void
main()
{
int
ar[10][10];
int
i,j,r=0,n,size,c=0;
clrscr();
printf("\n\tEnter
Size : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
ar[i][j]=0;
}
}
size=n;
i=1;
while(i<=size*size)
{
for(j=0;j<n;j++)
{
ar[r][c++]=i++;
}
r++;
c--;
if(i<=size*size)
{
for(j=0;j<n-1;j++)
{
ar[r++][c]=i++;
}
r--;
c--;
}
if(i<=size*size)
{
for(j=0;j<n-1;j++)
{
ar[r][c--]=i++;
}
r--;
c++;
}
if(i<=size*size)
{
for(j=0;j<n-2;j++)
{
ar[r--][c]=i++;
}
r++;
c++;
}
n=n-2;
}
printf("\n\tThe array->\n");
for(i=0;i<size;i++)
{
printf("\n\t");
for(j=0;j<size;j++)
{
printf("%3d
",ar[i][j]);
}
}
getch();
}
Spiral Matrix - Inbound
#include <stdio.h>
#include <conio.h>
void main()
{
int
ar[10][10];
int
i,j,r=0,n,size,c=0;
clrscr();
printf("\n\tEnter
Size : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
ar[i][j]=0;
}
}
size=n;
i=size*size;
c=r=size-1;
while(i>=1)
{
for(j=0;j<n;j++)
{
ar[r--][c]=i--;
}
r++;
c--;
if(i>=1)
{
for(j=0;j<n-1;j++)
{
ar[r][c--]=i--;
}
r++;
c++;
}
if(i>=1)
{
for(j=0;j<n-1;j++)
{
ar[r++][c]=i--;
}
r--;
c++;
}
if(i>=1)
{
for(j=0;j<n-2;j++)
{
ar[r][c++]=i--;
}
r--;
c--;
}
n=n-2;
}
printf("\n\tThe array->\n");
for(i=0;i<size;i++)
{
printf("\n\t");
for(j=0;j<size;j++)
{
printf("%3d
",ar[i][j]);
}
}
getch();
}
1 comment:
Great กV I should certainly pronounce, impressed with your site. I had no trouble navigating through all tabs and related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your client to communicate. Excellent task..
Double End Bag
Post a Comment