-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoDmatrixRotation.cpp
121 lines (109 loc) · 3.25 KB
/
TwoDmatrixRotation.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Author: Hemant Tripathi
*/
#include<iostream>
using namespace std;
#define MAXSIZE 5
void rotateClockwise(int arr[][MAXSIZE], int start, int end);
void rotateAntiClockwise(int arr[][MAXSIZE], int start, int end);
int main() {
bool isExit = false;
if(MAXSIZE < 2) {
cout << "Minimum 2D Array Size must be greater than 1"<<endl;
return 0;
}
int arr[MAXSIZE][MAXSIZE];
cout << "################# Program of 2D Matrix Rotation ################" << endl;
while(1) {
if(isExit)
break;
cout << "Enter your choice" << endl;
cout << "1. Create 2D Matrix" << endl;
cout << "2. Rotate 90 degree clockwise" << endl;
cout << "3. Rotate 90 degree anticlockwise" << endl;
cout << "4. Print 2D Matrix"<<endl;
cout << "5. Exit"<<endl;
int selection;
cin >> selection;
int temp;
switch(selection) {
case 1:
cout << "Enter data into " << MAXSIZE<<"*"<<MAXSIZE<<" matrix"<<endl;
for(int i=0; i < MAXSIZE; i++) {
for(int j=0; j < MAXSIZE; j++) {
cout << "Enter arr["<<i<<"]["<<j<<"] data:"<<endl;
cin >> arr[i][j];
}
}
cout << "Matrix filled successfully"<<endl;
break;
case 2:
cout << "Rotating to 90 degree clockwise"<<endl;
rotateClockwise(arr, 0, MAXSIZE-1);
cout <<"Clockwise rotation done!"<<endl;
break;
case 3:
cout << "Rotating to 90 degree anticlockwise"<<endl;
rotateAntiClockwise(arr, 0, MAXSIZE-1);
cout <<"AntiClockwise rotation done!"<<endl;
break;
case 4:
cout << "Printing 2D matrix"<<endl;
for(int i=0; i < MAXSIZE; i++) {
for(int j=0; j < MAXSIZE; j++) {
cout << "\t" << arr[i][j];
}
cout << endl;
}
break;
case 5:
isExit = true;
break;
}
}
cout << "Terminating the program"<<endl;
return 0;
}
void rotateClockwise(int arr[][MAXSIZE], int start, int end) {
int temp;
int size = end-start;
cout << "Size = "<<size<<"; start = "<<start<<"; end = "<<end<<endl;
if(size < 1) {
return;
}
for(int j=start; j < end; j++) {
cout << "j = "<<j<<endl;
temp = arr[start][j];
cout << "Temp = "<<temp<<endl;
arr[start][j] = arr[end-j+start][start];
cout << "arr["<<start<<"]["<<j<<"] = "<<arr[start][j]<<endl;
arr[end-j+start][start] = arr[end][end-j+start];
cout<<"arr["<<end-j+start<<"]["<<start<<"] = "<<arr[end-j+start][start]<<endl;
arr[end][end-j+start]=arr[j][end];
cout<<"arr["<<end<<"]["<<end-j+start<<"] = "<<arr[end][end-j+start]<<endl;
arr[j][end] = temp;
cout<<"arr["<<j<<"]["<<end<<"] = "<<arr[j][end]<<endl;
}
rotateClockwise(arr, start+1, end-1);
}
void rotateAntiClockwise(int arr[][MAXSIZE], int start, int end) {
int temp;
int size = end-start;
cout << "Size = "<<size<<"; start = "<<start<<"; end = "<<end<<endl;
if(size < 1) {
return;
}
for(int j=start; j < end; j++) {
cout << "j = "<<j<<endl;
temp = arr[start][j];
cout << "Temp = "<<temp<<endl;
arr[start][j] = arr[j][end];
cout << "arr["<<start<<"]["<<j<<"] = "<<arr[start][j]<<endl;
arr[j][end] = arr[end][end-j+start];
cout<<"arr["<<j<<"]["<<end<<"] = "<<arr[j][end]<<endl;
arr[end][end-j+start] = arr[end-j+start][start];
cout<<"arr["<<end-j+start<<"]["<<start<<"] = "<<arr[end-j+start][start]<<endl;
arr[end-j+start][start] = temp;
}
rotateAntiClockwise(arr, start+1, end-1);
}