#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++) { // Do it square by square or layer by layer
for (int i = j; i < m - j -1; i++) {
cout << mat[j][i] << " ";
}
for (int i = j; i < m - j -1 ; i++) {
cout << mat[i][m - j -1] << " ";
}
for (int i = j; i < m - j -1; i++) {
cout << mat[m - j - 1][m - i - 1] << " ";
}
for (int i = j; i < m - j -1; i++) {
cout << mat[m - i -1][j] << " ";
}
cout << endl;
}
if (m % 2 != 0) { // for odd number of rows & columns
cout << mat[m/2][m/2] << endl;;
}
}
No comments:
Post a Comment