Question

In: Computer Science

Create a function called merge that takes two int vectors (x and y) as argument and...

Create a function called merge that takes two int vectors (x and y) as argument and returns an int vector (z) that is the result of merging the two vectors x and y. Here is an example to explain how the merge function works: If the vector x has the following values: 1 2 3 4 5, and the vector y has the following: 6 7 8 9 10 11 12, then the merging vector z will have: 1 6 2 7 3 8 4 9 5 10 11 12. This means: z[0] = x[0], z[1]=y[0], z[2]=x[1], z[3]=y[1], z[4]=x[2], z[5]=y[2],... If the size of the vector x is bigger than the size of the vector y, when x is entirely copied to z, the rest of x will be copied next. If y is bigger, the rest of y is copied when x is entirely copied to z.

Language: c++

Solutions

Expert Solution

Below is the complete C++ code. If you face any difficulty while understanding the solution, Please let me know in the comments.

Code:

#include <iostream>
#include <vector>

using namespace std;

vector<int> merge(vector<int> x, vector<int> y) {
  
vector<int> z;
  
// get the size of both the vectors
int size1 = x.size(), size2 = y.size();
int i = 0, j = 0;
  
// loop while both the vectors are not traversed completely
while(i < size1 || j < size2) {
  
// add current item from x vector
if(i < size1) {
z.push_back(x[i]);
i++;
}
  
// add currentitem from y vector
if(j < size2) {
z.push_back(y[j]);
j++;
}   
}
  
return z;
}

int main() {
  
vector<int> x = { 1, 2, 3, 4, 5 };
vector<int> y = { 6, 7, 8, 9, 10, 11, 12};
  
// call merge function
vector<int> z = merge(x,y);
  
// print the items of merged vector
for (auto& iterator : z)
cout << iterator << ' ';
  
return 0;
}

Screenshots:

Output:


Related Solutions

Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the area of a regular hexagon given the length of the side. Show that the function works in a worksheet by inserting the formula somewhere.
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
Write a C function hwkOneA, that takes a long int x as well as two integers...
Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
1.1 Write a python in Jupiter notebook function called trng that takes three numbers x, y,...
1.1 Write a python in Jupiter notebook function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangle otherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT