-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_matrix.cpp
More file actions
59 lines (52 loc) · 948 Bytes
/
rotate_matrix.cpp
File metadata and controls
59 lines (52 loc) · 948 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define N 4
void print(int arr[N][N])
{
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < N; ++j)
cout << arr[i][j] << " ";
cout << '\n';
}
}
void rotate(int arr[N][N])
{
// First rotation
// with respect to Secondary diagonal
for(int i = 0; i < N; i++)
{
for(int j = i; j < N; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
//print(arr);
// Second rotation
// with respect to middle row
for(int i=0; i<N; i++){
int l = 0;
int r = N - 1;
while(l < r){
int t = arr[i][l];
arr[i][l] = arr[i][r];
arr[i][r] = t;
l++, r--;
}
}
//print(arr);
}
// Driver code
int main()
{
int arr[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate(arr);
print(arr);
return 0;
}
// This code is contributed by Rahul Verma