Vector union

-2

How do I join 2 vectors in 1 vector? Without repeating the numbers?

Question: Make a program that reads two vectors of 10 elements. Create a vector that is the union between the 2 previous vectors, that is, that contains the numbers of the two vectors. It should not contain repeated numbers.

@edit: I saw some examples on the internet and I came to this but it's pretty wrong ;-)

#include <stdio.h>

int main (){
int vetA[2], vetB[2], vetC[4], i,l,j;


printf ("Informe 10 valores para o VETOR A:\n");
for (i=0; i<2; i++){
scanf ("%d", &vetA);
}

printf ("Informe 10 valores para o VETOR B:\n");
for (i=0; i<2; i++){
    scanf ("%d", &vetB);
}

printf ("\nA uniao e: ");
for (i=0; i<2; i++){
    if ((vetC[0] != vetA[i]) && (vetC[1] != vetA[i]) && (vetC[2] != vetA[i]))
    printf ("%d ", vetA[i]);
}

for (i=0; i<l; i++){
    printf ("%d ",vetC);
}
for (j=0; j<2; j++){
    if ((vetC[0] != vetB[i]) && (vetC[1] != vetB[i]) && (vetC[2] != vetB[i]))
    printf ("%d ", vetB[i]);
}
}
    
asked by anonymous 21.10.2017 / 21:31

1 answer

2

The vetC could start as having all elements of vetA and having 20 elements.

Then, put a for inside the other to choose which elements of vetB you put in vetC . The% of external% runs through for and internal runs through vetB .

Put a variable inside the vetA external call for and initialize it with 0. If in the achou of internal you find that the element of for (of vetB external) is equal to% (% of internal%), you change the value of for to 1. Use vetA to do this check.

After% internal%, even within% external%, use another for with variable found to decide if you put element achou in if .

You will also need a variable to track how many elements you put in for , since the size of for is variable depending on the number of replicates.

    
21.10.2017 / 22:03