In ogni caso ecco cosa intendeva:
codice:
#include <stdio.h>
#include <stdbool.h>
#define N 5
#define M 3
int *indexer(int [][M], int);
int main(int argc, char **argv) {
int j, k;
int mat[N][M];
int *e1, *e2;
int tmp;
bool change;
//inserimento dati
printf("Inserire una matrice di %ux%u elementi:\n", N, M);
for(j = 0; j < N; j++) {
for(k = 0; k < M; k++) {
scanf("%d", &mat[j][k]);
}
}
while(getchar() != '\n'); //pulisce il buffer dopo la lettura con scanf
//stampa dati
printf("\n\n");
for(j = 0; j < N; j++) {
for(k = 0; k < M; k++) {
printf("%d ", mat[j][k]);
}
printf("\n");
}
//ordinamento dati
for(j = N*M - 1; j > 0; j--) {
change = false;
for(k = 0; k < j; k++) {
e1 = indexer(mat, k);
e2 = indexer(mat, k + 1);
if((*e1) > (*e2)) {
tmp = *e1;
(*e1) = (*e2);
(*e2) = tmp;
change = true;
}
}
if(!change) break;
}
//stampa dati
printf("\n\n");
for(j = 0; j < N; j++) {
for(k = 0; k < M; k++) {
printf("%d ", mat[j][k]);
}
printf("\n");
}
getchar();
}
int *indexer(int mat[][M], int index) {
return &(mat[index/M][index%M]);
}
Il codice è pressochè uguale a quello del post precedente.