I am trying to display the values stored in a 4x5 matrix by rows and columns, separately. However, the columns are printed to me in the same order as the values stored in rows. In addition, in the last values returned they do not correspond and are a row of meaningless numbers. I don't know if it's the fault of using an online C compiler that doesn't handle so much data correctly, because only in one of the executions has it correctly printed the values stored by columns. What's going on?
#include <stdio.h>
#define m 4
#define n 5
int main () {
puts("Este programa almacenará los elementos de una matriz 4x5 y mostrará los valores de filas y columnas por separado.");
int matriz[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
printf("\nIntroduzca el elemento almacenado en la fila %d y columna %d: ", i+1, j+1);
scanf("%d", &matriz[i][j]);
}
}
puts("\n\nElementos almacenados en las distintas filas de la matriz: ");
for (int i=0; i<m; i++) {
printf("\nFILA %d:", i+1);
for (int j=0; j<n; j++) {
printf(" %d", matriz[i][j]);
}
}
puts("\n\nElementos almacenados en las distintas columnas de la matriz: ");
for (int j=0; j<n; j++) {
printf("\nCOLUMNA %d:", j+1);
for (int i=0; i<m; i++) {
printf(" %d", matriz[j][i]);
}
}
return 0;
}