PROBLEM 49: Matrix Rotate Problem
A program to rotate a square matrix by 90° anti-clockwise direction. (Interview Question)
(For best view experience, view in windows version)
Author : Ajay ZadDate : 19/04/2022
#include<stdio.h>void main(){ int i,j,r,c,m,n; printf("Enter the size for Row :"); scanf("%d",&r); printf("Enter the size for Column :"); scanf("%d",&c); int matrix1[r][c],matrix2[r][c]; printf("Enter the elements in the matrix :\n"); for(i = 0 ; i < r ; i++) { for(j = 0 ; j < c ; j++) { scanf("%d",&matrix1[i][j]); } }
printf("\n\n"); for(i = 0 ; i < r ; i++) { for(j = 0 ; j < c ; j++) { matrix2[i][j] = matrix1[j][i]; } }
m = 0; for(i = (r-1); i >= 0 ; i--) { n = 0; for( j = 0 ; j < c ; j++) { matrix1[m][n] = matrix2[i][j]; n++; } m++; }
printf("Matrix after 90 degree's rotation : \n"); for(i = 0 ; i < r ; i++) { for(j = 0 ; j < c ; j++) { printf("%d\t",matrix1[i][j]); } printf("\n"); } }
The above code is in C language.
Output :Enter the size for Row : 3Enter the size for Column : 3Enter the elements in the matrix :123456789
The original form of the matrix will be as follow :1 2 34 5 67 8 9
Matrix after 90 degree's rotation :3 6 92 5 81 4 7
No comments:
Post a Comment