Search for repeated numbers c

0

I have the following exercise:

Given a square matrix of whole numbers between 1 and 100 write a something- rhythm that detects all the numbers that are repeated and replace them by zero, indicating how many there are without repeating

I tried to do it like this:

#include <stdio.h>

#define C 3

int main( void ) {
  int matrizA[C][C];
  int i;
  int j;
  int v;
  char encontrado;
  int false;
  int true;

  v = matrizA[i][j];

  for( i = 1; i < C ; i++ ) {
    for( j = 1; j < C ; j++ ){
      printf( "\nintroduzca cada numero %d de la matriz cuadrada ", matrizA[i][j] );
      scanf( "%d", &matrizA[i][j] );
    }
    encontrado = false;

    for( i = 1; ( i < C ) && !encontrado; i++ ) {
      for( j = 1; ( j < C ) && !encontrado; j++ ) {
        if( matrizA[i][j] == v )
          encontrado = true;
      }
    }
  }

  return 0;
}

How do you see the code ?, is that when I run I get "bus error ('core' generated)"

Thanks

    
asked by Robert 25.03.2017 в 01:13
source

1 answer

1

Hello Robert Welcome.

look at the errors that I see in your code are the following, first I see that you assign to V the value of a variable that you have not yet initialized, being able to give any value, these are logic errors, on the other hand the cycle must start it in i = 0 and in j = 0 if you want to use it as an index of arrays, finally if you want to go through the matrix looking for the repeated ones I recommend going through the matrix once for each element, maybe there is a better solution but this one helped me. Greetings

#include<stdio.h>

#define C 3


 int main (void) {

  int matrizA[C][C];


  int i;
  int j;

  int v;
  char encontrado;
  int false;
  int true;


   //v = matrizA[i][j];
    //printf("El valor de v es %d, el valor de i es %d, y el valor de j es %d\n", v,i,j);
    /*
    * El array debes iniciarlo en 0 las posiciones de memoria van de 0 a 2 si son 3
    */
    //for (i=1;i<C ;i++) 
    for ( i = 0; i < C; i++)
    {
        for ( j = 0; j < C; j++)
        {
            // para "llenar" la matriz 
            printf ("\nintroduzca el indice %d, %d de la matriz cuadrada ", i+1, j+1);
         // printf ("\nintroduzca cada numero %d de la matriz cuadrada ", matrizA[i][j]);
          scanf ("%d",&matrizA[i][j]);
        }                                    
    }

// la variable encontrado hace que nunca entre al loop
  //encontrado = false;
//¿con que comparamos la matriz?
    int k, h;
    for ( k = 0; k < C; k++)
    {
        for (h = 0; h < C; h++)
        {
            v = matrizA [k][h];
            encontrado = false;
            for (i = 0; i < C; i++) 
            {
                for (j=0; j < C; j++) 
                {

                    if (matrizA[i][j] == v && (k != i || h != j ) && matrizA[i][j]!= 0)
                    {
                        matrizA[i][j] = 0;
                        //printf("El valor de v es %d, el valor de i es %d, y el valor de j es %d\n", v,i,j);
                        encontrado = true;

                    }



                }
            }
            if (encontrado)
                matrizA [k][h] = 0;
        }
    }

    // mostrando la matriz
    for ( i = 0; i < C; i++)
    {
        for ( j = 0; j < C; j++)
        {
            printf ("%d ", matrizA[i][j]);
        }                                 
        printf ("\n");
    }
}
    
answered by 25.03.2017 в 03:47