-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoD_ArrayMatrix90degreeRotationClockwise.c
More file actions
57 lines (57 loc) · 1.37 KB
/
Copy pathTwoD_ArrayMatrix90degreeRotationClockwise.c
File metadata and controls
57 lines (57 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//program to rotate a matrix by 90 degree clock-wise
#include <stdio.h>
int main(){
int n;
printf("Enter value for n : ");
scanf("%d",&n);
int arr[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%d",&arr[i][j]);
}
}
//print the user given matrix
printf("Original user given matrix : \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d",arr[i][j]);
}
printf("\n");
}
//transpose the given matrix and print it.
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
printf("Transposed of user given matrix : \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d",arr[i][j]);
}
printf("\n");
}
// rotate
for(int i=0; i<n; i++){
int j = 0;
int k = n-1;
while(j<k){
int temp = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = temp;
j++;
k--;
}
}
//print the rotated matrix
printf("Matrix after rotating 90 degree clock-wise : \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d",arr[i][j]);
}
printf("\n");
}
return 0;
}