Vector union, no repetitions in C

Asked

Viewed 1,505 times

0

Follows the statement:

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

  • Sum between 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 elements appearing in the two vectors;

  • Union between X and Y: all elements of X, and all elements of Y which 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 managing to make the union without repetition of elements.. Can anyone help me? Thank you

1 answer

1


You can do it that 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 values of the first vector are printed, after which each of the values of the second vector is compared to all values of the first vector (no function check), if no repetition occurs the printing.

  • 1

    Its algorithm is in quadratic order. The strategy needs to be done in a totally different way to be executed in linear order

Browser other questions tagged

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