forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateMatrixBy90.cpp
42 lines (39 loc) · 857 Bytes
/
RotateMatrixBy90.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
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void reverseColumns(int arr[R][C])
{
for (int i = 0; i < C; i++)
for (int j = 0, k = C - 1;
j < k; j++, k--)
swap(arr[j][i], arr[k][i]);
}
void transpose(int arr[R][C])
{
for (int i = 0; i < R; i++)
for (int j = i; j < C; j++)
swap(arr[i][j], arr[j][i]);
}
void printMatrix(int arr[R][C])
{
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++)
cout << arr[i][j] << " ";
cout << '\n';
}
}
void rotate90(int arr[R][C])
{
transpose(arr);
reverseColumns(arr);
}
int main()
{
int arr[R][C] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
rotate90(arr);
printMatrix(arr);
return 0;
}