Solution in C/C++ - compiler g++
#include <iostream>
using namespace std;
main() {
int m = 0;
cout << "Enter number of rows & columns in square matrix " ;
cin >> m;
int mat[m][m];
cout << "Enter elements \n";
for (int k = 0; k < m; k++) {
for (int l = 0; l < m; l++) {
cin >> mat[k][l];
}
}
for (int j = 0; j < m/2; j++) {
for (int i = j; i < m - j -1; i++) {
int temp1 = 0, temp2 = 0;
temp1 = mat[i][m - j - 1];
mat[i][m - j - 1] = mat[j][i];
temp2 = mat[m - j - 1][m - i - 1];
mat[m - j - 1][m - i - 1] = temp1;
temp1 = mat[m - i -1][j];
mat[m - i -1][j] = temp2;
mat[j][i] = temp1;
}
}
cout << "Elements after 90 degree rotation \n";
for (int k = 0; k < m; k++) {
for (int l = 0; l < m; l++) {
cout << mat[k][l] << " ";
}
}
cout << endl;
}
Execution result:
Enter number of rows & columns in square matrix 4
Enter elements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Elements after 90 degree rotation
13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4
No comments:
Post a Comment