fork download
  1.  
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5. void rotate(vector < vector < int >> & matrix) {
  6. int n = matrix.size();
  7. //transposing the matrix
  8. for (int i = 0; i < n; i++) {
  9. for (int j = 0; j < i; j++) {
  10. swap(matrix[i][j], matrix[j][i]);
  11. }
  12. }
  13. //reversing each row of the matrix
  14. for (int i = 0; i < n; i++) {
  15. reverse(matrix[i].begin(), matrix[i].end());
  16. }
  17. }
  18.  
  19. int main() {
  20. vector < vector < int >> arr;
  21. arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  22. rotate(arr);
  23. cout << "Rotated Image" << endl;
  24. for (int i = 0; i < arr.size(); i++) {
  25. for (int j = 0; j < arr[0].size(); j++) {
  26. cout << arr[i][j] << " ";
  27. }
  28. cout << "n";
  29. }
  30.  
  31. }
  32.  
  33.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Rotated Image
7 4 1 n8 5 2 n9 6 3 n