#include <stdio.h>
#include <stdlib.h>
int main()
{
int nr,nc,i,j;
int k = 0, l = 0;
int **A;
printf("Enter the number of row\n");
scanf("%d",&nr);
printf("Enter the number of coloumn\n");
scanf("%d",&nc);
//Dynamic memory Allocation for 2D-Array
A=(int**)malloc(nr*sizeof(int*));
for(i=0;i<nr;i++)
{
A[i]=(int*)malloc(nc*sizeof(int));
}
for(i=0;i<nr;i++)
{
for(j=0;j<nc;j++)
{
A[i][j]=(rand()%100)+1; //Random number between 1 to 100
}
}
// Printing 2D Matrix
for(i=0;i<nr;i++)
{
for(j=0;j<nc;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("\n");
printf("Spiral printing\n");
// Printing in Spiral order
while (k < nr && l < nc)
{
for (i = l; i < nc; ++i)
{
printf("%d ", A[k][i]);
}
k++;
for (i = k; i < nr; ++i)
{
printf("%d ", A[i][nc-1]);
}
nc--;
if ( k < nr)
{
for (i = nc-1; i >= l; --i)
{
printf("%d ", A[nr-1][i]);
}
nr--;
}
if (l < nc)
{
for (i = nr-1; i >= k; --i)
{
printf("%d ", A[i][l]);
}
l++;
}
}
return 0;
}
#include <stdlib.h>
int main()
{
int nr,nc,i,j;
int k = 0, l = 0;
int **A;
printf("Enter the number of row\n");
scanf("%d",&nr);
printf("Enter the number of coloumn\n");
scanf("%d",&nc);
//Dynamic memory Allocation for 2D-Array
A=(int**)malloc(nr*sizeof(int*));
for(i=0;i<nr;i++)
{
A[i]=(int*)malloc(nc*sizeof(int));
}
for(i=0;i<nr;i++)
{
for(j=0;j<nc;j++)
{
A[i][j]=(rand()%100)+1; //Random number between 1 to 100
}
}
// Printing 2D Matrix
for(i=0;i<nr;i++)
{
for(j=0;j<nc;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("\n");
printf("Spiral printing\n");
// Printing in Spiral order
while (k < nr && l < nc)
{
for (i = l; i < nc; ++i)
{
printf("%d ", A[k][i]);
}
k++;
for (i = k; i < nr; ++i)
{
printf("%d ", A[i][nc-1]);
}
nc--;
if ( k < nr)
{
for (i = nc-1; i >= l; --i)
{
printf("%d ", A[nr-1][i]);
}
nr--;
}
if (l < nc)
{
for (i = nr-1; i >= k; --i)
{
printf("%d ", A[i][l]);
}
l++;
}
}
return 0;
}
0 comments:
Post a Comment