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

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 it's already giving error

    
asked by anonymous 07.04.2018 / 23:03

3 answers

1

Instead of using a third loop to assemble the union of vectors, you can get 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 target (hence for the 1st position of c on my first use and 11th on the second use.) The second is pointer to the source, and the third is the pointer to the source. size of the data you want to copy.

See working on Ideone .

    
08.04.2018 / 21:52
0

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

In the loop that fills c , you should have done a conditional, if it is less than 10, get a , otherwise get b , only calculating the position of the vector where it is taking and for Where are you going?

This calculation is in CiroboyBR's response, and another option would be:

link

#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;
}
    
08.04.2018 / 20:58
-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;
}
    
08.04.2018 / 03:39