Răspuns :
Răspuns:
#include <iostream>
using namespace std;
int main() {
//citire matrice
int n = 3, mat[3][3];
cout << "Introduceti elementele matricei:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "mat[" << i << "][" << j << "]= ";
cin >> mat[i][j];
}
}
cout << endl;
//afisare matrice
cout << "Matrice[3][3]:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
// sortare matrice desc pe coloane prin metoda insertiei
int x, p;
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int x = mat[j][i];
int p = j - 1;
while (p >= 0 && mat[p][i] < x) {
mat[p+1][i] = mat[p][i];
p--;
}
mat[p+1][i] = x;
}
}
//afisare matrice
cout << endl;
cout << "Matrice sortat descrescator prin metoda insertiei:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
Explicație: