Wednesday 27 January 2016

Print Elements of a Matrix in Spiral Order

Write a program that reads an MxN matrix A and prints its elements in spiral order.
You should start from the element in the 0th row and 0th column in the matrix and proceed in a spiral order as shown below.

1→ 2 → 3 → 4                       
                      ↓
5 → 6 → 7 8
↑             ↓
9    10←11 12
↑             
13←14←15←16

Output for the above matrix: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

INPUT:
First line contains two integers M and N separated by whitespace. The next M lines contain elements of matrix A, starting with the topmost row. Within each row, the elements are given from left to right.
OUTPUT:
Elements of the matrix printed in a spiral order. All the elements should be separated by whitespace.

CONSTRAINTS:
1 <= M <= 5, 1 <= N <= 5.

Elements in the matrix will be in the range [-100,100]


Solution-:
NOTE- in this you will get presentation error however if you submit you will get 100.0 score
presentation error is because of the white-space in the end .


#include<stdio.h> int main() {int m,n,l=0,k=0,j,i,a[5][5]; scanf("%d",&m); scanf("%d",&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } while(k<m && n>l) {for(i=l;i<n;i++) {printf("%d ",a[k][i]); } k++; for(i=k;i<m;i++) { printf("%d ",a[i][n-1]); }n--; if(k<m) {for(i=n-1;i>=l;i--) printf("%d ",a[m-1][i]); m--;} if(l<n) { for(i=m-1;i>=k;i--) printf("%d ",a[i][l]); l++;} } return 0;}

No comments:

Post a Comment