Find grid number

1

Help, I need to solve this program, I've been thinking for days and I can not find any solution:

I need to find the cell:

I would enter the number of rows and columns and another variable that will be "p" which will be the number to find.

//Localizacion de la celda

#include <stdio.h>
#include <math.h>

int main(int argc, char const *argv[])
{
    int n,m,p,res,fil,col;

    printf("Introduce el nombre de filas: ");
    scanf("%d",&n);
    printf("Introduce el nombre de columnas: ");
    scanf("%d",&m);
    printf("Escribe el valor de p: ");
    scanf("%d",&p);

    // NO logro avanzar mas
    return 0;
}
    
asked by Pepe 06.07.2017 в 22:04
source

4 answers

1

Taking advantage of your variables, I think of something like:

int matriz[n][m];

// Rellenas la matriz como creas conveniente

int f,c, ok=0;
for( f = 0; f < n && !ok; f++) {
  for( c = 0; c < m && !ok; c++) {
    if(matriz[f][c] == p)
      ok = 1;
  }
}

if( ok )
  printf("El número se encuentra en las coordenadas (%d,%d)\n",f,c);
else
  printf("No se ha encontrado el número\n");

Although for the future I would recommend you make use of names of more descriptive variables. Improve the readability of the code and this prevents you from making silly mistakes.

    
answered by 10.07.2017 в 17:32
1
  • 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!

    
answered by 12.07.2017 в 18:43
0

I did not try it, as much pull compilation error, but it should serve you to see the algorithm:

#include <stdio.h>

int main(int argc, char const *argv[]) {    

    int fil, col, numeroAbuscar;

    printf("Introduce el nombre de filas: ");
    scanf("%d",&fil);
    printf("Introduce el nombre de columnas: ");
    scanf("%d",&col);
    printf("Escribe el valor que quiere buscar: ");
    scanf("%d",&numeroAbuscar);

    int matriz[filas][columnas]; // Declaro la matriz

    // LLENO LA MATRIZ...

    // Busco
    int i, j, ok = 0;
    for(i = 0; i < filas && !ok; i++) { // Mientras no sobrepase la ultima fila y no haya encontrado el numero
        for(j = 0; j < columas && !ok; j++) { // Mientras no sobrepase la ultima columna y no haya encontrado el numero
            if(matriz[i][j] == numeroABuscar)
                ok = 1;
        }
    }

    if(ok)
        printf("El numero fue encontrado en la fila %d y la columna %d\n", i, j);
    else
        printf("El numero no se encuentra en la matriz\n");
    return 0;
}

I hope I have been of help.

Greetings!

    
answered by 06.07.2017 в 22:21
0

If the cells in the matrix are numbered as the image you provided then the row and column number will be given by:

    columna = p%col;
    fila = p/col;

I hope it serves you.

    
answered by 10.07.2017 в 17:23