Union of vectors, without repetitions in C [duplicate]

0

The statement follows:

  

Read two vectors of X and Y integers, each one with 5   elements (assume that the user does not report repeated elements).   Calculate and show the resulting vectors in each case below:

     
  • Sum of X and Y: sum of each element of X with the element of the same position in Y;

  •   
  • Product between X and Y: multiplication of each element of X with the element of the same position in Y;

  •   
  • Difference between X and Y: all elements of X that do not exist in Y;

  •   
  • Intersection between X and Y: only the elements that appear in the two vectors;

  •   
  • Link between X and Y: all elements of X, and all elements of Y that are not in X.

  •   

code:

#include <stdio.h>
#include <locale.h>
#define TAM 5


int main(){
    setlocale(LC_ALL,"Portuguese");

    int x[TAM], y[TAM], dif[TAM], c, c2, c3, n=0, uniao[TAM*2], aux=0;

    printf("Lendo o vetor X: \n");
    for (c=0; c<TAM; c++){
        scanf("%d", &x[c]);
    }

    printf("\n\nLendo o vetor Y: \n");
    for (c=0; c<TAM; c++){
        scanf("%d", &y[c]);
    }

    system("CLS");

    printf("Soma entre X e Y:\n");
    for (c=0; c<TAM; c++){
        printf("%d\n", x[c] + y[c]);
    }

    printf("\n\nProduto entre X e Y:\n");
    for (c=0; c<TAM; c++){
        printf("%d\n", x[c] * y[c]);
    }

    printf("\n\nDiferenca ente X e Y:\n");
    for(c=0; c<TAM; c++){
        for(c2=0; c2<TAM; c2++){
            if(x[c] == y[c2])
                break;
        }

        if(c2==TAM){
            for(c3=0; c3<n; c3++){
                if(x[c] == dif[c])
                    break;
            }

            if(c3 == n)
                dif[n++] = x[c];
        }
    }

    for (c=0; c<n; c++){
        printf("%d\n", dif[c]);
    }

    printf("\n\nIntersecção entre X e Y:\n");
    for (c=0; c<TAM; c++){
        for (c2=0; c2<TAM; c2++){
            if (x[c] == y[c2]){
                printf("%d\n", x[c]);
            }
        }
    }

    printf("\n\nUnião entre X e Y:\n");
}

I am not able to make the union without repeating elements .. can anyone help me? Thank you

    
asked by anonymous 19.07.2018 / 03:56

1 answer

1

You can do it this way:

#include <stdio.h>
#include <stdlib.h>
#define TAM 5

int verificar(int elemento,int vetor[],int tamanho);

int main()
{
int vet1[TAM];
int vet2[TAM];
int i;

for(i = 0; i < TAM; i++){
    printf("Digite o valor de vet1[%d]:",i);
    scanf("%d",&vet1[i]);
}

printf("\n\n");

for(i = 0; i < TAM; i++){
    printf("Digite o valor de vet2[%d]:",i);
    scanf("%d",&vet2[i]);
}

system("cls");

printf("Valores: ");
for(i = 0; i < TAM; i++){
    printf("%d ",vet1[i]);
}

for(i = 0 ; i < TAM ; i++){

    if(verificar(vet2[i],vet1,TAM)){
       printf("%d ",vet2[i]);
    }

}

return 0;
}


int verificar(int elemento,int vetor[],int tamanho){

int j;
for(j = 0; j < tamanho; j++){
    if(elemento == vetor[j]){
        return 0;
    }
}
 return 1;
}

In the above code, all the values of the first vector are printed, after that each of the values of the second vector is compared to all the values of the first vector (no check function). p>     

19.07.2018 / 05:07