Write a function interleave which takes two vectors of the s

Write a function interleave which takes two vectors of the same length and returns a vector which is the result of interleaving the elements of the two input vectors as shown in the examples below, interleaved([1 2 3], [10 11 12]) = [1 10 2 11 3 12] interleave([1], [-5]) = [1 -5] interleave([2 3 4], [-1 -2 -3 -4]) = [1 -1 2 -2 3 -3 4 -4] Write another function interleave2 which behaves like interleave but can also handle the case of two vectors of different length, as shown in the examples below. interleave2([1 2 3], [-5]) = [1 -5 2 3] interleave2([1 2], [-1 -2 -3]) = [1-1 2 -2 -3] Describe the testing cases you will use for interleave2, and explain why you chose them.

Solution

#include <iostream>
#include<vector>
using namespace std;

vector<int> interlace_vectors1(const vector<int>& v1, const vector<int>& v2)
{
vector<int> result;
for(size_t n = 0; n < v1.size(); ++n)
{
result.push_back(v1[n]);
result.push_back(v2[n]);
}
return result;
}
vector<int> interlace_vectors2(const vector<int>& v1, const vector<int>& v2)
{
vector<int> result;
if(v1.size() ==0)
return v2;
if(v2.size() ==0)
return v1;

for(size_t n = 0; n < max(v1.size(), v2.size()); ++n)
{
if(n < v1.size()) result.push_back(v1[n]);
if(n < v2.size()) result.push_back(v2[n]);
}
return result;
}
int main()
{
size_t size = 10;
std::vector<int> a1(size);
std::vector<int> a2(size);
a1={1,2,3};
a2={11,22,33,44,55};
vector<int> result = interlace_vectors2(a1,a2);
for(size_t n = 0; n < result.size(); ++n)
{
cout << result[n] << \" \";
}
return 0;
}

 Write a function interleave which takes two vectors of the same length and returns a vector which is the result of interleaving the elements of the two input v

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site