Fill a vector of size[10] with two other vectors[5] at positions 0, 2, 4, 6, 8 and 1, 3, 5, 7, 9

Asked

Viewed 318 times

0

Question:

Create two vectors with size 5 and a size 10 vector;

Fill the two size 5 vectors with integers inserted keyboard;

After filling the two vectors of 5 positions their content should be passed to the vector of 10 positions so that:

  • Use a repeat loop in reading and passing values;

  • The contents of the first vector should be in positions 0, 2, 4, 6, 8;

  • The contents of the second vector should be in positions 1, 3, 5, 7, 9.

I just don’t know how to pass these two size 5 vectors in those size 10 vector positions.

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(){
    int vet1[5], vet2[5], vet3[10];

    for (int i=0; i<5; i++){
        //cout << "Primeiro vetor de tamanho 5: \n";
        cout << "Informe um valor: ";
        cin >> vet1[i];
    }

    for (int i=0; i<5; i++){
        //cout << "\nSegundo vetor de tamanho 5: \n";
        cout << "Informe um valor 2: ";
        cin >> vet2[i];
    }
  • Hi Magazak, all right? As this has college exercise face I will try to help you with the problem without giving the final solution. If you need more help tell me. The secret of this exercise is that you need to make a decision within the loop. Even indexes (ie where i % 2 == 0) shall be read from vet1 while odd indices should be read from vet2. Another issue is that you need to map the indexes between 0 and 9 vet3 for their indices between 0 and 4 vet1 and vet2. You can do it by splitting i for two, e.g. vet3[i] = vet1[i / 2].

  • 1

    Or, do the 0 to 4, and put the vet1 and vet2 elements in 2i and 2i + 1 of vet3...

  • 1

    An alternative is to do: for (i=0; i<5;i++) { vet3[2*i] = vet1[i]; vet3[2*i+1] = vet2[i];}.

  • Thank you all so much for your help, I got it

  • Nick looks like my hein kkkk

  • Really? Kkkkkkk what is your?

Show 1 more comment
No answers

Browser other questions tagged

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