Rotate Matrix 90 Degree Clockwise ID:2668
A MxN matrix is passed as the input. The program must rotate the matrix by 90 degrees in clock wise direction and print the rotated matrix as the output.
Input Format:
First line will contain the value of M.
Second line will contain the value of N.
Next M lines will contain the N values with each value separated by one or more space.
Output Format:
N lines will contain the M values with each value separated by one or more space.
Boundary Conditions:
2 <= M <= 15
2 <= N <= 15
Example Input/Output 1:
Input:
2
3
4 5 9
1 3 5
Output:
1 4
3 5
5 9
Example Input/Output 2:
Input:
4
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
#include <stdio.h>
int main(int argc, char** argv)
{
int m,n;
scanf("%d %d",&m,&n);
int a[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=m-1;j>=0;j--)
{
printf("%d ",a[j][i]);
}
printf("\n");
}
}
Comments
Post a Comment