Advertisement
totobac

Untitled

Jan 11th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void swapCounterClockwise(int& a, int& b, int& c, int& d)
  7. {
  8.     int temp = a;
  9.     a = d;
  10.     d = c;
  11.     c = b;
  12.     b = temp;
  13. }
  14.  
  15. void swapClockwise(int& a, int& b, int& c, int& d)
  16. {
  17.     int temp = a;
  18.     a = b;
  19.     b = c;
  20.     c = d;
  21.     d = temp;
  22. }
  23.  
  24. void rotate(int matrix[][10], int m, int n)
  25. {
  26.     int counter = 0;
  27.     n -= 1;
  28.     for (size_t row = 0; row < n; row++)
  29.     {
  30.         for (size_t col = 0; col < n; col++)
  31.         {
  32.             if (row % 2 == 0 )
  33.             {
  34.  
  35.                 if (row == col && row <= n / 2)
  36.                 {
  37.                     swapClockwise(matrix[row][col], matrix[n - counter][counter],
  38.                         matrix[n - counter][n - counter], matrix[counter][n - counter]);
  39.                     counter++;
  40.                 }
  41.             }
  42.  
  43.             else
  44.             {
  45.                 if (row == col && row <= n / 2)
  46.                 {
  47.                     swapCounterClockwise(matrix[row][col], matrix[n - counter][counter],
  48.                         matrix[n - counter][n - counter], matrix[counter][n - counter]);
  49.                     counter++;
  50.                 }
  51.             }
  52.  
  53.         }
  54.     }
  55. }
  56.  
  57. void printMatrix(int matrix[10][10], int n)
  58. {
  59.     for (size_t i = 0; i < n; i++)
  60.     {
  61.         cout << endl;
  62.         for (size_t j = 0; j < n; j++)
  63.         {
  64.             cout << matrix[i][j] << " ";
  65.         }
  66.     }
  67. }
  68. int main()
  69. {
  70.     int n , m;
  71.     cout << "Enter the matrix's size: ";
  72.     cin >> n;
  73.     cout << "Enter how many times the matrix to rotate: ";
  74.     cin >> m;
  75.    
  76.    
  77.     int matrix[10][10];
  78.     cout << "Enter the matrix: " << endl;
  79.     for (size_t i = 0; i < n; i++)
  80.     {
  81.         cout << endl;
  82.         for (size_t j = 0; j < n; j++)
  83.         {
  84.             cin >> matrix[i][j];
  85.         }
  86.     }
  87.    
  88.     int numberOfRotations = 0;
  89.     while(numberOfRotations < m)
  90.     {
  91.         rotate(matrix, m, n);
  92.         numberOfRotations++;
  93.     }
  94.     printMatrix(matrix, n);
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement