Question
I just need help with part 1.Part 2 is optional, you may want to do part2 to verify your function for part 1 works.
Write a function with header given as follows: The merged vector is returned. In the merged vector, the elements are: v0[0] v1[0] v0[1] v1[1] v0[2] v1[2] v0[3] v1[3] ... If the number of elements in v0 is less than that in v1, for example, there are 2 elements in v0, but there are 5 elements in v1, then the elements in the merged vectors are: v0[0] v1[0] v0[1] v1[1] v1[2] v1[3] v1[4] . If the number of elements in v0 is more than that in v1, for example, there are 4 elements in v0, but there are 2 elements in v1, then the elements in the merged vectors are: v0[0] v1[0] v0[1] v1[1] v0[2] v0[3] . This function should be defined in a file with name Write a main function to generate three vectors vec0, vec1, vec2 with int type random elements (each element is in the range [2, 15]). The sizes of vecO, vecl and vec2 are 2, 3 and 7, respectively. Print out those three vectors. We call the function merge_vector to merge vec0 and vec1; call the function merge_vector to merge vec2 and vec1. The main function prints out those two merged vectors. The main function should be defined in a file with name Note that your program should use time (0) in function strand () to set the seed (otherwise, 1 marks would get deducted). Some sample output:
Explanation / Answer
vector<int> merge_vector(vector<int> v0, vector<int> v1)
{
int i,j,k;
vector<int> v2;
for(i=0,j=0,k=0; i<v0.size() && j<v1.size(); k++)
{
if(k%2==0)
{
v2.push_back(v0[i]);
i++;
}
else
{
v2.push_back(v1[j]);
j++;
}
}
while(i<v0.size() )
{
v2.push_back(v0[i]);
i++;
}
while(j<v1.size() )
{
v2.push_back(v1[j]);
j++;
}
return v2;
}