Please do this in C++. Write a function called splice () that “splices” two inte
ID: 3861673 • Letter: P
Question
Please do this in C++.
Write a function called splice() that “splices” two integer arrays together by first allocating memory for a dynamic array with enough room for both integer arrays, and then copying the elements of both arrays to the new array as follows:
first, copy the elements of the first array up to a given position,
then, copy all the elements of the second array,
then, copy the remaining elements in the first array.
The function should have parameters for the two input arrays, their lengths, and the number of elements of the first array to be copied before the second is spliced. The function should return a pointer to the new array.
To test the function, write a program that:
Prompts the user for the sizes of the two integer arrays and the number of
elements of the first array to be copied before the splice (e.g., 32 means insert
32 elements of the first array and then the second array),
Creates the two arrays and fills them with random values. Use the seed 100 for
srand().
Outputs the contents of the two input arrays in rows with 10 values/row.
Outputs the contents of the spliced array in rows with 10 values/row.
The program should use dynamic memory to store all three arrays. Be sure to de-allocate the memory before exiting. Your program should run and terminate with no run-time errors.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int *splice(int v1[], int len1,
int v2[], int len2, int pos);
int main()
{
Count << “ * * * Testing the splice function * * * ”
<< endl;
int I, len1 = 10, len2 = 5;
int *a1 = new int[len1],
*a2 = new int[len2];
Srand((unsigned)time(NULL));
for( i=0;i< len1; ++i)
a1[i] = rand();
for( i=0;i< len2; ++i)
a2[i] = -rand();
count << “1.array: ” << endl;
for( i=0; i< len1; ++i)
count << setw(12) << a1[i];
count << endl;
count << “2.array: ” << endl;
for( i=0; i< len2; ++i)
count << setw(12) << a2[i];
count << endl;
count << “ At what position do you want to insert”
“ the 2nd array into 1st array?”
“ Possible positions: 0,1, , “len1
<< “ : ”;
Int pos; cin >> pos;
}