Vector union in the language C

Asked

Viewed 1,662 times

0

Question: Make a program that reads two vectors of 10 elements. Create a vector that is the union between the two previous vectors, i.e., 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 join vectors , the algorithm starts by placing the values of the vector A in the vector B and then should test the values of B and the equals not to put in the vector of the union someone gives a help there

1 answer

2


The problem is something very small, this:

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

You can’t keep it based on v. Imagining that the array B starts with 3 repeated values the first to be saved went to the house 13 instead of 10, for the v always increases whether guard or not. It has to guard based on the ate which symbolizes the amount of elements already stored and consequently the last:

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

Watch it work on Ideone

The logic you used turned out to be a bit complicated too, and it also doesn’t work in case the A array itself already has repeated elements.

You can also solve this case and keep the code simple by adding 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 the variables of the for, making each one with a different one. This is not necessary and can use the same at all because when the declaration is made within the for this variable exists only within it.

  • Mt thanks really worth it

Browser other questions tagged

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