unite 2 vectors (a[10], b[10]) into a vector c[20]

Asked

Viewed 314 times

0

How do I join two vectors A[10] and B[10], to create a vector C with 20 elements.

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

int main(){

    int a[10];
    int b[10];
    int c[20];
    int i;

    srand(time(NULL));

    for(i = 0; i < 10; i++){
        a[i] = rand()%101;
    }
    for(i = 0; i < 10; i++){
        b[i] = rand()%101;
    }
    for(i = 0; i < 20; i++){
        c[i] = rand()%101;
    }

    printf("%d\n" a[i]);
    printf("%d\n" b[i]);
    printf("%d\n" c[i]);
}

for now I’m trying to print the 3 but is already giving error

3 answers

1


Instead of using a third loop to assemble the vector union, it is possible to obtain the same result with memcpy:

#include <stdio.h>

int main(){

    int a[10];
    int b[10];
    int c[20];
    int i;

    srand(time(NULL));

    for(i = 0; i < 10; i++){
        a[i] = rand()%101;
    }
    for(i = 0; i < 10; i++){
        b[i] = rand()%101;
    }

    // Montagem de c
    memcpy(c, a, 10 * sizeof (int));
    memcpy(c + 10, b, 10 * sizeof (int));

    // Exibição dos resultados
    for(i = 0; i < 20; i++){
        printf("%d\n", c[i]);
    }

}

The first argument of memcpy is a pointer to the destination (therefore to the 1st position of c in my first use and 11th position in the second use. The second is pointer to the origin, and the third is the size of the data you want to copy.

See it working on Ideone.

0

You even came close in your attempt, but time to copy to the vector c, you did not take the value of the other vectors, but generated another randomly.

There in the loop that fills the c, you should have made a parole, if you’re under 10, taken from a, else caught from b, just calculating the position of the vector where it is picking up and where it goes.

This calculation is in the answer of Ciroboybr, and another option would be:

https://ideone.com/KdvYYZ

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

typedef int vint10[10];
typedef int vint20[20];

int main(void) {
    vint10 a, b;
    vint20 c;
    srand(time(NULL));
    for(int i = 0; i < 10; i++) {
        a[i] = rand() % 100;
        b[i] = rand() % 100;
        printf("a[%02d]=%2d, b[%02d]=%2d\n", i, a[i], i, b[i]);
    }
    printf("---\n");
    for(int i = 0; i < 20; i++) {
        c[i] = i < 10 ? a[i] : b[i-10];
        printf("c[%02d]=%2d\n", i, c[i]);
    }
    return 0;
}

-1

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

int main()
{

int i, a[10], b[10], c[20];

srand(time(NULL));

for(i = 0; i < 10; i++)
{
    a[i] = rand()%20;
    b[i] = rand()%20;
}

for(i = 0; i < 10; i++ )
    c[i] = a[i];
for(i = 0; i < 10; i++ )
    c[i+10] = b[i];

printf("\nA: ");
for(i = 0; i < 10; i++ )
    printf("%d ", a[i]);

printf("\nB: ");
for(i = 0; i < 10; i++ )
    printf("%d ", b[i]);

printf("\nC: ");
for(i = 0; i < 20; i++ )
    printf("%d ", c[i]);

return 0;
}
  • running on the https://ideone.com/4lb41V

Browser other questions tagged

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