-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrotate90degree_CW.cpp
More file actions
80 lines (64 loc) · 1.7 KB
/
rotate90degree_CW.cpp
File metadata and controls
80 lines (64 loc) · 1.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//Question link : https://youtu.be/0qj4kRcuKqo?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk
//Rotate a matrix by 90 degress Clock-Wise
//The Matrix is a square Matrix
#include <iostream>
#include<algorithm>
#include <vector>
using namespace std;
vector<vector<int> > input()
{
// takes input in a 2d vector and return a 2d vector
int r;
cin >> r;
int c=r;
vector<vector<int> > arr(r, vector<int>(c, 0));
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
cin >> arr[i][j];
}
return arr;
}
void printMatrix(vector<vector<int> > &arr1)
{
int r1 = arr1.size();
int c1 = arr1[0].size();
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
cout << arr1[i][j] << " ";
cout << "\n";
}
}
void rotate90(vector<vector<int> > &arr)
{
//The matrix is a sqaure matrix
//Rotating 90 degree CW is achieved by first transforming the matrix and then reversing each row of the matrix
int n=arr.size();
//First we will transform
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
//we will be doing it only for either lower or upper traingle as if we perform for all element then matrix will
//remain same only
if(i>j)
swap(arr[i][j],arr[j][i]);
}
}
cout<<"\nAfter Transformation the matrix is : \n";
printMatrix(arr);
for(int i=0;i<n;i++)
{
reverse(arr[i].begin(),arr[i].end());
}
cout<<"\nMatrix after rotating 90 degree clockwise is : \n";
printMatrix(arr);
}
int main()
{
vector<vector<int> > arr1 = input();
cout << "Original input : \n";
printMatrix(arr1);
rotate90(arr1);
}