fork download
  1. // C++ program for left rotation of matrix by 90
  2. // degree wuthout using extra space
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define R 4
  6. #define C 4
  7.  
  8. // After transpose we swap elements of column
  9. // one by one for finding left rotation of matrix
  10. // by 90 degree
  11. void reverseColumns(int arr[R][C])
  12. {
  13. for (int i=0; i<C; i++)
  14. for (int j=0, k=C-1; j<k; j++,k--)
  15. swap(arr[j][i], arr[k][i]);
  16. }
  17.  
  18.  
  19. // Function for do transpose of matrix
  20. void transpose(int arr[R][C])
  21. {
  22. for (int i=0; i<R; i++)
  23. for (int j=i; j<C; j++)
  24. swap(arr[i][j], arr[j][i]);
  25. }
  26.  
  27. // Function for print matrix
  28. void printMatrix(int arr[R][C])
  29. {
  30. for (int i=0; i<R; i++)
  31. {
  32. for (int j=0; j<C; j++)
  33. cout << arr[i][j] << " ";
  34. cout<<'\n';
  35. }
  36. }
  37.  
  38. // Function to anticlockwise rotate matrix
  39. // by 90 degree
  40. void rotate90(int arr[R][C])
  41. {
  42. transpose(arr);
  43. reverseColumns(arr);
  44. }
  45.  
  46. //Driven code
  47. int main()
  48. {
  49. int arr[R][C]= { {1, 2, 3, 4},
  50. {5, 6, 7, 8},
  51. {9, 10, 11, 12},
  52. {13, 14, 15, 16}
  53. };
  54. rotate90(arr);
  55. printMatrix(arr);
  56. return 0;
  57. }
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
4 8 12 16 
3 7 11 15 
2 6 10 14 
1 5 9 13