-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathmatrix_rotation.cpp
62 lines (59 loc) · 1.02 KB
/
matrix_rotation.cpp
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
58
59
60
61
62
/*
Rotation of matrix through a specified angle
input
2
1 2
3 4
90
output
3 1
4 2
*/
#include<iostream>
using namespace std;
int matrix[100][100],temp[100][100];
int n;
void rotate(int angle)
{
int i,j;
int total = angle/90;
total = total % 4;
while(total--)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
temp[j][n-i-1]=matrix[i][j];
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matrix[i][j]=temp[i][j];
}
}
int main(int argc, char const *argv[])
{
int i,j,angle,totalrot=0;
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>matrix[i][j];
}
}
cin>>angle;
rotate(angle);
totalrot = totalrot + angle;
// rotate(360 - (totalrot%360)); // to convert the rotated matrix to the original one
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}