Could someone tell me what's wrong with my code?
To start, as you say @Xam, in C the indexes start at 0 (really an index indicates a displacement on the first element, that is, it is more an offset than an index.
So the loops should be programmed like this:
for (c = 0; c != 2;c++){
printf("Por favor, ingrese numeros ENTEROS hasta llenar la matriz %i\n",c);
for (a = 0 ;a != 3; a++){
for(b = 0; b != 3 ; b++){
And with this small change you should already work ... but as questions about things that are wrong in your code we can continue.
Reduces the life of the variables to the minimum
From C99 (standard dating from 1999), you can declare variables in almost any part of the program (even within the loops). Reducing the life of the variables to the minimum essential is a good idea since the program becomes more readable and reduces the chances of messing up when reusing a variable:
for (int c = 0; c != 2;c++){
printf("Por favor, ingrese numeros ENTEROS hasta llenar la matriz %i\n",c);
for (int a = 0 ;a != 3; a++){
for(int b = 0; b != 3 ; b++){
If you're only interested in reading integers do not use float
scanf(" %f",&num);
if (num != (int)num) puts("Asegurese de ingresar un numero ENTERO");
A problem associated with decimal numbers is that they have a certain maximum precision that, in the case of float
, is 6 digits. This means that a variable of type float
is not going to give too well to store the number 100,000.001 since that number exceeds the aforementioned 6 digits of precision.
If the program should only accept the entry of integers then consider reading those integers and that's it ... or else you read the entry as a text string and verify if that string is fully convertible to a whole number.
Why do I tell you this? Because your program as it is is not infallible and for that it is enough to enter a letter in any of the positions of the matrix and the program will go crazy.
So, since I do not think that right now you are required to validate the user's input, go to the simple and do not get complicated. Change this:
do {
scanf(" %f",&num);
if (num != (int)num) puts("Asegurese de ingresar un numero ENTERO");
}
while (num != (int)num);
mat[a][b][c] = (int)num;
Because of this:
scanf(" %d",&mat[a][b][c]);
It's shorter, cleaner and will work just as well.
Encode this program that consists of reading 2 arrays of whole numbers
Then this definition for the matrices:
int mat[3][3][2]
It is not exactly the cleanest solution since in memory you are mixing the data of both matrices (row 0 of the first matrix, row 0 of the second matrix, row 1 of the first matrix, row 1 of the second matrix, ...) This solution will prevent you from treating each matrix independently, although it is not something that they are asking you right now, they will ask for it in the near future and better if you acquire good habits.
Also, the comment you include:
//En vez de utilizar dos matrices bidimensionales, utilicé una matriz tridimensional (para poder anidar todo en el mismo ciclo)
It does not suppose a justification that is defended too well.
On the one hand they are not telling you to store the matrices anywhere ... just store an average of certain values.
int main(){
int promedio[2] = {0}; //En vez de utilizar dos matrices bidimensionales, utilicé una matriz tridimensional (para poder anidar todo en el mismo ciclo)
for (int i = 0; i < 2; i++){
printf("Por favor, ingrese numeros ENTEROS hasta llenar la matriz %i\n",i+1);
for (int x = 0 ; x < 3; x++){
for(int y = 0; y < 3 ; y++){
int valor;
scanf(" %d",&valor);
if( x != y )
promedio[i] += valor;
}
}
promedio[i] /= 6;
}
if (promedio[0] == promedio[1])
printf("Los promedios de los valores NO diagonales de ambas matrices es igual: %i\n",promedio[0]);
else
printf("El promedio de los valores NO DIAGONALES de ambas matrices es diferente \nMATRIZ1: %i\nMATRIZ2: %i\n",promedio[0],promedio[1]);
return 0;
}