- First of all I do not see the need to include the library
math.h
, so that you can get it out.
- In the second place you declared several variables that you do not use, in addition to good programming practice, it is always best to use explanatory identifiers, that is, when reading the name of the variables you immediately understand what their function is, that is why you will use
filas
, columnas
and numero
.
- Finally, since you want the number of rows and columns to be entered in execution time, you will have to use the function
malloc
found in the library stdlib.h
to reserve the memory space your matrix will use (and release it later), and you will also have to handle pointers (in the C99 standard you can make use of int arreglo[tamano]
to declare a variable size array with tamano
an entire variable, however from the C11 standard the power to do this is not is insured and is optional, so it will depend on the decision of the compiler that you use if int arreglo[tamano]
is valid or not, that's why as mentioned above the best option is to use malloc
).
Considering the above, I wrote the following code.
#include <stdio.h>
#include <stdlib.h>
int main() {
int **matriz;
int i, j, filas, columnas, numero;
printf("Introduce el nombre de filas: ");
scanf("%d", &filas);
printf("Introduce el nombre de columnas: ");
scanf("%d", &columnas);
printf("Escribe el valor del numero a encontrar: ");
scanf("%d", &numero);
matriz = malloc(columnas * sizeof(int *));
for (i = 0; i < filas; i++)
matriz[i] = malloc(columnas * sizeof(int));
for (i = 0; i < filas; i++)
for (j = 0; j < columnas; j++) {
printf("Ingrese que numero desea ingresar en la celda [%d, %d]: ", i, j);
scanf("%d", &matriz[i][j]);
}
for (i = 0; i < filas; i++)
for (j = 0; j < columnas; j++)
if (matriz[i][j] == numero)
printf("El numero se encontro en la fila %d y en la columna %d\n", i, j);
for (i = 0; i < filas; i++)
free(matriz[i]);
free(matriz);
return 0;
}
If you have any doubt, consult or criticize, you can ask without fear.
Greetings and luck!