Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void swapCounterClockwise(int& a, int& b, int& c, int& d)
- {
- int temp = a;
- a = d;
- d = c;
- c = b;
- b = temp;
- }
- void swapClockwise(int& a, int& b, int& c, int& d)
- {
- int temp = a;
- a = b;
- b = c;
- c = d;
- d = temp;
- }
- void rotate(int matrix[][10], int m, int n)
- {
- int counter = 0;
- n -= 1;
- for (size_t row = 0; row < n; row++)
- {
- for (size_t col = 0; col < n; col++)
- {
- if (row % 2 == 0 )
- {
- if (row == col && row <= n / 2)
- {
- swapClockwise(matrix[row][col], matrix[n - counter][counter],
- matrix[n - counter][n - counter], matrix[counter][n - counter]);
- counter++;
- }
- }
- else
- {
- if (row == col && row <= n / 2)
- {
- swapCounterClockwise(matrix[row][col], matrix[n - counter][counter],
- matrix[n - counter][n - counter], matrix[counter][n - counter]);
- counter++;
- }
- }
- }
- }
- }
- void printMatrix(int matrix[10][10], int n)
- {
- for (size_t i = 0; i < n; i++)
- {
- cout << endl;
- for (size_t j = 0; j < n; j++)
- {
- cout << matrix[i][j] << " ";
- }
- }
- }
- int main()
- {
- int n , m;
- cout << "Enter the matrix's size: ";
- cin >> n;
- cout << "Enter how many times the matrix to rotate: ";
- cin >> m;
- int matrix[10][10];
- cout << "Enter the matrix: " << endl;
- for (size_t i = 0; i < n; i++)
- {
- cout << endl;
- for (size_t j = 0; j < n; j++)
- {
- cin >> matrix[i][j];
- }
- }
- int numberOfRotations = 0;
- while(numberOfRotations < m)
- {
- rotate(matrix, m, n);
- numberOfRotations++;
- }
- printMatrix(matrix, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement