In the program the 2 D array whose rows and columns are to be given at run time has to be displayed as a spiral i.e. from outer rows and columns to inner ones in clockwise direction .
The code is not working for rows > 4 and columns > 4 .
The scanf function is not reading correct value in the pointer.
Logic of program ( for clarity of source ):
1) store value in 2D array using pointer
2) display top row
3) storing its remaining values in another 2D array ( strored such that it spins as well )
4) displaying top row of this array
5) continuing till columns are more than 1.
Code:#include<stdio.h> #include<conio.h> void main() { int **ptr,**ptr1,i,j,row1,row,col; ptr1=NULL; clrscr(); printf("\nenter no of rows "); scanf("%d",&row); printf("\nenter no of columns "); scanf("%d",&col); printf("\nenter matrix\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d", &ptr[i][j] ); /* not reading correct value of input */ } } while(row>0 && col>0) { for(j=0;j<col;j++) printf("%d ",ptr[0][j]); if(row>1) { for(i=0;i<col;i++) { for(j=0;j<row-1;j++) { ptr1[i][j]=ptr[j+1][(col-1)-i]; } } } row1=row; row=col; col=row1-1; ptr=ptr1; } getch(); }