Single elements of an array

2
When reading an array of order n x m where neither are greater than 0 and less than or equal to 10, I want to read the elements of this array and check which are the only elements of this array, but when passing element by element, I'm not getting the desired return, my idea is to transform that array into a vector of size n * and then go comparing it, here's my code:

#include <stdio.h>

  int main ()
  {
    int cont=0, l, c, i, j, m, n, mat[10][10], vet[100], h=0;

   // leitura das dimensoes da matriz 
   scanf ("%d %d" ,&l ,&c);

   //caso as dimensoes nao sejam do tamanho desejado pelo exercicio 
   if (l<=0||l>10||c<=0||c>10){
   printf("dimensao invalida\n");
   return 0;
   }



   else 
   {

    //leitura dos elementos da matriz    
    for (i=0;i<l;i++)
    {
        for (j=0;j<c;j++)
        {
          scanf("%d" ,&mat[i][j]);
        }
    }

    //transformacao da matriz em um vetor 
    for (i=0;i<l;i++)
    { 
        for (j=0;j<c;j++)
        {
          vet[h] = mat[i][j];
          h = h + 1; 
        }
    }

    //comparacao de elemento por elemento 
    for (i = 0; i < (l*c); i++) 
    {
        for (j = i + 1; j < (l*c); j++) 
        {
            if (vet[i] != vet[j] && i!=j) 
            {
            printf("%d" ,vet[i]);
            cont++;
            }
        }
    }

    printf(" %d" ,cont);

    }    
    return 0;
    }

I know the problem is in the comparison of arrays, however my question is: Do I have to use a function that compares the elements of my vector and consequently use a pointer? Can I make this comparison with the elements of my array without converting it to a vector?

    
asked by anonymous 17.11.2018 / 04:39

1 answer

2

The logic that is in the last for does not serve what you intend, this:

for (i = 0; i < (l*c); i++) {
    for (j = i + 1; j < (l*c); j++) {
        if (vet[i] != vet[j] && i!=j) {
            printf("%d",vet[i]);
            cont++;
        }
    }
}

Here you go through each element and for each element in front of it, if it is different account one. Then imagining that it has the% vector of% It will count 3 when it goes in the first, count two when it goes in the second and thereafter. This does not represent the logic you want.

In addition, moving the entire array to the vector is unnecessary and only complicates the solution. A simple solution is to pass each element of the array to the vector if that element does not yet exist.

Example:

#include <stdio.h>

int existe(int vet[], int tamanho, int valor){
    int i;
    for (i = 0; i < tamanho; ++i){
        if (vet[i] == valor){
            return 1;
        }
    }
    return 0;
}

int main () {
    int l, c, i, j, mat[10][10], vet[100], unicos=0;

    scanf ("%d %d",&l,&c);
    if (l <= 0 || l > 10 || c <= 0 || c > 10){
        printf("dimensao invalida\n");
    }
    else {
        for (i=0; i<l; i++){
            for (j=0; j<c; j++){
                scanf("%d",&mat[i][j]);
            }
        }

        for (i = 0; i < l; i++) {
            for (j = 0; j < c; j++) {
                //aqui passa cada elemento para o vetor de unicos apenas se não existir lá
                if (!existe(vet, unicos, mat[i][j])){
                    vet[unicos++] = mat[i][j];
                }
            }
        }
        printf("%d", unicos);
    }
    return 0;
}

Try Ideone

    
17.11.2018 / 11:51