#include<stdio.h>
int k=0,res[100]={0},flag=0,a[100][100]={0},n,m;
void spiral(){
int i,row;
row=0;
while(row<=n/2){
for(i=row;i<m-1-row;i++){
res[k]=a[row][i];
printf(" %d",res[k]);
k++;
}
for(i=row;i<n-1-row;i++){
res[k]=a[i][m-1-row];
printf(" %d",res[k]);
k++;
}
for(i=m-1-row;i>row && row<n/2;i--){
res[k]=a[n-1-row][i];
printf(" %d",res[k]);
k++;
}
for(i=n-1-row;i>row;i--){
res[k]=a[i][row];
printf(" %d",res[k]);
k++;
}
row++;
}
if(flag==1 && (n==m)){
printf(" %d",a[n/2][n/2]);
}
if(flag==1 && (n!=m))
printf(" %d",a[n-row][m-row]);
}
// DRIVER PROGRAM
void main(){
int i,j;
//clrscr();
scanf("%d %d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
// CHECK IF NUMBER OF ROW IS ODD OR EVEN
if(n%2!=0)
flag=1;
else
flag=0;
spiral();
//getch();
}
int k=0,res[100]={0},flag=0,a[100][100]={0},n,m;
void spiral(){
int i,row;
row=0;
while(row<=n/2){
for(i=row;i<m-1-row;i++){
res[k]=a[row][i];
printf(" %d",res[k]);
k++;
}
for(i=row;i<n-1-row;i++){
res[k]=a[i][m-1-row];
printf(" %d",res[k]);
k++;
}
for(i=m-1-row;i>row && row<n/2;i--){
res[k]=a[n-1-row][i];
printf(" %d",res[k]);
k++;
}
for(i=n-1-row;i>row;i--){
res[k]=a[i][row];
printf(" %d",res[k]);
k++;
}
row++;
}
if(flag==1 && (n==m)){
printf(" %d",a[n/2][n/2]);
}
if(flag==1 && (n!=m))
printf(" %d",a[n-row][m-row]);
}
// DRIVER PROGRAM
void main(){
int i,j;
//clrscr();
scanf("%d %d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
// CHECK IF NUMBER OF ROW IS ODD OR EVEN
if(n%2!=0)
flag=1;
else
flag=0;
spiral();
//getch();
}
No comments:
Post a Comment