Union of two vectors in C

Asked

Viewed 6,645 times

1

I need to perform the union of two vectors, resulting in a third vector:

a[5]={1, 2, 3, 4, 5}, b[5]={6, 7, 8, 9, 10}, c[10];
else if (select_menu == 4) {
    int select_f4, i, j, x;
    printf("Esta é uma opção que realiza a união dos conjuntos\n");
    printf("Resultado da união entre os dois vetores, com os números inseridos até o momento: \n");
    for(i=0; i<5; i++){
        c[i] = a[i];
    }
    for (j=0; j<5; j++){
        c[i] = b[j];
    }
    for (x=0; x<10; x++){
        printf("%d, ", c[x]);
    }
}

The result should be:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Instead, you stay:

1, 2, 3, 4, 5, 10, 257, 0, 1152, 0,

  • As at the end of the first is the variable i comes out with the value 5 just put c[i+j] = b[j]; in the second is.

3 answers

5

No tie is needed for to solve your problem!

You can use the function memcpy() of the standard library string.h to unite the two vectors into a third vector, see only:

int a[5] = { 1, 2, 3, 4, 5 };
int b[5] = { 6, 7, 8, 9, 10 };
int c[10];

/* Copia vetor 'a' para a primeira metade do vetor 'c' */
memcpy( c, a, sizeof(a) );

/* Copia vetor 'b' para a segunda metade do vetor 'c' */
memcpy( c + 5, b, sizeof(b) );

See working on Ideone.com

Alternatively, you can make this union independent of the size of the input vectors, provided that the output vector has the sum of the sizes of the input vectors:

int a[7] = { 1, 2, 3, 4, 5, 6, 7 };
int b[3] = { 8, 9, 10 };
int c[10];

/* Copia vetor 'a' para a primeira porção do vetor 'c' */
memcpy( c, a, sizeof(a) );

/* Copia vetor 'b' para a segunda porção do vetor 'c' */
memcpy( c + (sizeof(a)/sizeof(int)), b, sizeof(b) );

See working on Ideone.com

  • There’s only one caveat in this alternative where the size of the vector is not given: "size independent" as long as they are vectors; it does not work if I pass a pointer.

3


Just exchange c[i] = b[j]; for c[j + 5] = b[j];.

After all, the first 5 items are for a and the last 5 to b. Without this + 5, you would put the b above those of a. However, how were you accessing the c with the variable i and the b with the j, was placing all the elements of b (is worth the last, which is 10) in c[5].

These numbers 257, 0, 1152 and 0 are probably rubbish.

One way to avoid errors like this is to declare variables only in the smallest necessary scopes, which in your case is only in the for. For example:

        for (int i = 0; i < 5; i++) {
            c[i] = a[i];
        }
        for (int j = 0; j < 5; j++) {
            c[i] = b[j]; // O compilador vai mostrar que aqui há um erro!
            c[j + 5] = b[j]; // Isso seria o correto.
        }
        for (int x = 0; x < 10; x++) {
            printf("%d, ", c[x]);
        }
  • 1

    Thank you very much!! It worked!

0

Simple, in the second for you are not adding the variable i, to be clearer to your way of understanding:

for(i=0; i<5; i++){
    c[i] = a[i];
}
for (j=0; j<5; j++){
    c[i] = b[j];
    i++;//desta forma você atualiza a posição em que será inserido no vetor c
}
for (x=0; x<10; x++){
    printf("%d, ", c[x]);
}

the values that were being visualized different were garbage of the positions in the vector c

Browser other questions tagged

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