Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Matrix rotation
//Title of this code #include <iostream> using namespace std; const int n = 4; const int m = 4; /* Rotate by +90: Transpose Reverse each row Rotate by -90: Transpose Reverse each column Rotate by +180: Method 1: Rotate by +90 twice Method 2: Reverse each row and then reverse each column Rotate by -180: Method 1: Rotate by -90 twice Method 2: Reverse each column and then reverse each row Method 3: Reverse by +180 as they are same */ void rotateMatrix(int t[n][m]) { int dest[n][m]; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) dest[j][n - i - 1] = t[i][j]; // copy the matrix for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) t[i][j] = dest[i][j]; } void printMatrix(int t[n][m]) { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cout << t[i][j] << " "; } cout << endl; } } int main() { int dest[n][m]; int t[n][m] = { {5, 1, 2, 6}, {1, 1, 9, 3}, {1, 1, 2, 3}, {8, 1, 2, 7} }; printMatrix(t); rotateMatrix(t); //rotateMatrix(t); //rotateMatrix(t); //rotateMatrix(t); cout << endl; printMatrix(t); }
run
|
edit
|
history
|
help
0
Rectangle overlap
C++ Car Racing game framework 2
PrePostIncrOp
Reverese every K node in list
Boost adapters foreach
Stack
Print Euler Path
Dijkstra
Find value in sorted matrix
ShoppingList