-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_6.cpp
66 lines (56 loc) · 1.07 KB
/
1_6.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
/* Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? */
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void transpose(int arr[][4], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
swap(arr[i][j], arr[j][i]);
}
}
for(int i = 0; i < n/2; i++)
{
for(int j = 0; j < n; j++)
{
swap(arr[i][j], arr[n - i -1][j]);
}
}
}
int main()
{
int matrix[4][4] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}
};
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
transpose(matrix, 4);
cout << endl << "After transpose, the matrix looks like this way: " << endl << endl;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
// system("pause");
return 0;
}