Union of vectors in the C language

0

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.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int vetorA[10];
    int vetorB[10];
    int vetorAB[20];
    int ate = 0;


    for(int i = 0; i < 10; i++){
        printf("Digite um valor para o vetorA[%d]:", i);
        scanf("%d",&vetorA[i]);
    }
    printf("\n");
    for(int j = 0; j < 10; j++){
        printf("Digite um valor para o vetorA[%d]:", j);
        scanf("%d",&vetorB[j]);
    }
    printf("\n");

    for(int k = 0; k < 10; k++){
        vetorAB[k] = vetorA[k];
    }

    for(int v = 0; v < 10; v++){
        int achou = 0;
        for(int z = 0; z < 10; z++){
            if(vetorB[v] == vetorA[z]){
                achou = 1;
                break;
            }
        }
        if(achou == 0){
            vetorAB[v+10] = vetorB[v];
            ate = ate + 1;
        }
    }
    for(int u = 0; u < 10 + ate; u++){
        printf("%d ", vetorAB[u]);
    }

    printf("\n");
    system("PAUSE");
    return 0;
}

I wanted to do a union of vectors, the algorithm starts by putting the values of vectorA in the vectorAB and then I had to test the values of B and the equals do not put in the vector of the union someone from a help there

    
asked by anonymous 13.11.2018 / 02:34

1 answer

2

The problem is something very small, this:

if(achou == 0){
    vetorAB[v+10] = vetorB[v];
    //      ^----
    ate = ate + 1;
}

Can not save based on v . Imagining that the B array begins with 3 repeated values, the first one to be saved goes to 13 instead of 10 , because v always increases either save or not. You have to save based on the ate that symbolizes the amount of elements already saved and consequently the last one:

if(achou == 0){
    vetorAB[10 + ate] = vetorB[v];
    //             ^----
    ate = ate + 1;
}

See it working on Ideone

The logic you used turned out to be a bit tricky too, and it also does not work if the array A itself already has repeated elements.

You can also resolve this case and keep the code simple if you add some functions to the operations you are doing.

Example:

#include <stdio.h>
#include <stdlib.h>

void le_array(int arr[], int tam, char *texto){
    for(int i = 0; i < 10; i++){
        printf("Digite um valor para o %s[%d]:", texto, i);
        scanf("%d",&arr[i]);
    }
    printf("\n");
}

int existe(int arr[], int tam, int val){
    for (int i = 0; i < tam; ++i) {
        if (arr[i] == val){
            return 1;
        }
    }
    return 0;
}

int main() {
    int vetorA[10], vetorB[10], vetorAB[20];
    int ate = 0;

    le_array(vetorA, 10, "vetorA");
    le_array(vetorB, 10, "vetorB");
    for(int v = 0; v < 10; v++){
        if (!existe(vetorAB, ate, vetorA[v])){
            vetorAB[ate++] = vetorA[v];
        }
    }
    for(int v = 0; v < 10; v++){
        if (!existe(vetorAB, ate, vetorB[v])){
            vetorAB[ate++] = vetorB[v];
        }
    }

    for(int u = 0; u < ate; u++){
        printf("%d ", vetorAB[u]);
    }
    return 0;
}

See this solution also in Ideone

I also noticed that you changed all variables of for , each with a different one. This is not necessary and can use the same at all because when the declaration is made within for this variable only exists inside it.

    
13.11.2018 / 04:28