Skip to content

Commit f32d8ee

Browse files
committed
Added cpp implementation of rotateTile(Day25)
1 parent 795b67e commit f32d8ee

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

day25/C++/rotateTile.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @author : Rajdeep Roy Chowdhury<[email protected]>
3+
* @handle : razdeep
4+
* @date : Feb 17, 2019
5+
*/
6+
#include <bits/stdc++.h>
7+
std::vector<std::vector<int>> rotate(std::vector<std::vector<int>> &two_D_array)
8+
{
9+
std::vector<std::vector<int>> result(two_D_array.size());
10+
for(int i=0; i<two_D_array.size();i++)
11+
{
12+
std::vector<int> this_array(two_D_array.size(), 0);
13+
result[i] = this_array;
14+
}
15+
int rj = two_D_array.size() - 1, ri = 0;
16+
for (int i = 0; i < two_D_array.size(); i++)
17+
{
18+
for (int j = 0; j < two_D_array[i].size(); j++)
19+
{
20+
result[ri++][rj] = two_D_array[i][j];
21+
}
22+
rj--;
23+
ri = 0;
24+
}
25+
return result;
26+
}
27+
int main(int argc, char **argv)
28+
{
29+
int n;
30+
std::cout << "Enter the value of n ";
31+
std::cin >> n;
32+
std::vector<std::vector<int>> two_D_array(n);
33+
for (int i = 0; i < n; i++)
34+
{
35+
std::vector<int> this_array(n, 0);
36+
// std::vector<int> this_array = new std::vector<int>(n);
37+
for (int j = 0; j < n; j++)
38+
{
39+
std::cin >> this_array[j];
40+
}
41+
two_D_array[i] = this_array;
42+
}
43+
std::cout << "Original Matrix is..." << std::endl;
44+
for (int i = 0; i < two_D_array.size(); i++)
45+
{
46+
for (int j = 0; j < two_D_array[i].size(); j++)
47+
{
48+
std::cout << two_D_array[i][j] << " ";
49+
}
50+
std::cout << std::endl;
51+
}
52+
std::vector<std::vector<int>> rotated_2d_array = rotate(two_D_array);
53+
std::cout << "Rotated matrix is" << std::endl;
54+
for (int i = 0; i < two_D_array.size(); i++)
55+
{
56+
for (int j = 0; j < two_D_array[i].size(); j++)
57+
{
58+
std::cout << rotated_2d_array[i][j] << " ";
59+
}
60+
std::cout << std::endl;
61+
}
62+
return 0;
63+
}

day25/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,72 @@ public class rotateTile {
158158
}
159159
}
160160
}
161+
```
162+
## C++ Implementation
163+
164+
### [Solution](./C++/rotateTile.cpp)
165+
```cpp
166+
/**
167+
* @author : Rajdeep Roy Chowdhury<[email protected]>
168+
* @handle : razdeep
169+
* @date : Feb 17, 2019
170+
*/
171+
#include <bits/stdc++.h>
172+
std::vector<std::vector<int>> rotate(std::vector<std::vector<int>> &two_D_array)
173+
{
174+
std::vector<std::vector<int>> result(two_D_array.size());
175+
for(int i=0; i<two_D_array.size();i++)
176+
{
177+
std::vector<int> this_array(two_D_array.size(), 0);
178+
result[i] = this_array;
179+
}
180+
int rj = two_D_array.size() - 1, ri = 0;
181+
for (int i = 0; i < two_D_array.size(); i++)
182+
{
183+
for (int j = 0; j < two_D_array[i].size(); j++)
184+
{
185+
result[ri++][rj] = two_D_array[i][j];
186+
}
187+
rj--;
188+
ri = 0;
189+
}
190+
return result;
191+
}
192+
int main(int argc, char **argv)
193+
{
194+
int n;
195+
std::cout << "Enter the value of n ";
196+
std::cin >> n;
197+
std::vector<std::vector<int>> two_D_array(n);
198+
for (int i = 0; i < n; i++)
199+
{
200+
std::vector<int> this_array(n, 0);
201+
// std::vector<int> this_array = new std::vector<int>(n);
202+
for (int j = 0; j < n; j++)
203+
{
204+
std::cin >> this_array[j];
205+
}
206+
two_D_array[i] = this_array;
207+
}
208+
std::cout << "Original Matrix is..." << std::endl;
209+
for (int i = 0; i < two_D_array.size(); i++)
210+
{
211+
for (int j = 0; j < two_D_array[i].size(); j++)
212+
{
213+
std::cout << two_D_array[i][j] << " ";
214+
}
215+
std::cout << std::endl;
216+
}
217+
std::vector<std::vector<int>> rotated_2d_array = rotate(two_D_array);
218+
std::cout << "Rotated matrix is" << std::endl;
219+
for (int i = 0; i < two_D_array.size(); i++)
220+
{
221+
for (int j = 0; j < two_D_array[i].size(); j++)
222+
{
223+
std::cout << rotated_2d_array[i][j] << " ";
224+
}
225+
std::cout << std::endl;
226+
}
227+
return 0;
228+
}
161229
```

0 commit comments

Comments
 (0)