Unique elements of a matrix

Asked

Viewed 51 times

2

When reading an order matrix n x m where n and m are greater than 0 and minor or iguas at 10, I want to read the elements of this matrix and verify which are the unique elements of this matrix, but when passing element by element, I am not getting the desired return, my idea is to transform this matrix into a vector of size n*m and then compare 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 lies in comparing matrices, however my question is: Do I have to use a function that compares the elements of my vector and therefore use a pointer ? I can make this comparison with the elements of my matrix without converting it to a vector ?

1 answer

2

The logic of 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 that, if it’s different count one. Then imagine that you have the vector [1, 2, 3, 4] You will count 3 when you go in the first, count two when you go in the second and so on. This does not represent the logic you want.

In addition, passing from the whole matrix to the vector is unnecessary and only complicates the solution. A simple solution is to pass each element of the matrix 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;
}

Testing at Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.