0
I have to make an algorithm in C that takes user values and armzazena in 2 different vectors (X and Y) and then shows the union (all elements of X and Y without repetitions), the difference (all elements of X that do not exist in Y, without repetitions). But I’m struggling to find the repeated values of the vectors. I found several codes for this purpose but I am not able to understand the logic. Follow what I have done:
int main(int argc, char** argv) {
int TAM = 3; //Variavel tamanho para facilitar os testes com valores menores
int x[TAM], y[TAM];
int uniao[TAM * 2], uniao_SR[TAM * 2]; //SR: Sem repetições
int i, j, p = 0;
printf("VETOR X\n");
for (i = 0; i < TAM; i++) {
printf("\tDigite o %dº número: ", i + 1);
scanf("%d", &x[i]);
uniao[i] = x[i]; //União dos dois vetores
}
printf("VETOR Y\n");
for (i = 0; i < TAM; i++) {
printf("\tDigite o %dº número: ", i + 1);
scanf("%d", &y[i]);
uniao[i + TAM] = y[i]; //União dos dois vetores
}
//Tentativa falha de eliminar os valores repetidos
for (i = 0; i < TAM * 2; i++) {
for (j = 1; j < TAM * 2; j++) {
if (uniao[i] != uniao[j]) {
uniao_SR[p] = uniao[j];
p++;
}
}
}