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 ?