Question: In C++ Define a function int * merge (int * p1, int len1, int * p2, int len2) that merges the arrays where the first
In C++
Define a function int * merge (int * p1, int len1, int * p2, int len2) that "merges" the arrays where the first array has base address p1 with length len1 and the second array has base address p2 with length len2. Additionally, the elements of both arrays are guaranteed to be in ascending order. The function should return a dynamically allocated array containing all the merged elements in ascending order. To merge, we start with the zero element of p1 or p2, whichever is smaller, followed by the next smallest element and so on until the elements of (at least) one of the arrays are exhausted. If there are still elements remaining to process from the other array, then we just tack them on at the end. The resulting array will contain all the elements from p1 and p2 in ascending order. You must use pointer notation throughout the function (no square brackets). Do not use shuffle and then sort the result. That's too slow!
As an example, suppose merge is called with the arrays nums1 and nums2 in the code below. Also suppose len1 and len2 are both 5.
Then the return array will have length 10 and contents {1, 3, 4, 6, 9 ,13, 14, 17, 18, 20}. #include
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
