How to create a vector by joining among others 2

2

I'm doing an exercise with vectors where I need to read two vectors of 5 positions each, and after that, concatenate these two in a 3 of 10 positions.

My program is printing the first vector correctly, but in the second it prints 4, 5, 5, 2686760, 1987092020 (I think they are addresses).

Here are the functions:

int criaVetor3(int v[], int v1[]){

int v3[10];

for(i=0; i<=4; i++){
    v3[i] = v[i];
}

for(i=4; i<=9; i++){ // Começa no 5 elemento do vetor e vai até o 10
    v3[i] = v1[i-5];
}
}

int mostraVetor3(int v[], int v1[]){

int v3[10];

for(i=0; i<=9; i++){
    printf("O numero posicao [%d] e %d\n", i, v[i]);
}

}
    
asked by anonymous 20.08.2017 / 20:40

1 answer

0

This code does not even compile. Solving these problems was fine, I just styled better.

It was missing a variable declaration and it was missing to return or pass a coverage vector, which I preferred to do not to involve dynamic allocation.

#include <stdio.h>

void criaVetor(int v[], int v1[], int v2[]) {
    for (int i = 0; i < 5; i++) {
        v[i] = v1[i];
    }
    for (int i = 0; i < 5; i++) {
        v[i + 5] = v2[i];
    }
}

void mostraVetor(int v[]) {
    for (int i = 0; i < 10; i++) {
        printf("O numero posicao [%d] e %d\n", i, v[i]);
    }
}

int main(void) {
    int v[10];
    int v1[] = {1, 2, 3, 4, 5};
    int v2[] = {6, 7, 8, 9, 10};
    criaVetor(v, v1, v2);
    mostraVetor(v);
}

See running on ideone . And on Coding Ground . Also I put GitHub for future reference .

    
20.08.2017 / 20:55