In: Computer Science
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 must ensure that non-numeric and negative entries are not entered. Use modular design in your program. That is, separate functions should be used to fill the arrays with random numbers, print the arrays, and of course, splice the arrays together.
#include<iostream>
#include<cstdlib>
#include<limits>
using namespace std;
//function to splice the two arrays
int *splice(int *arr1, int *arr2, int n1, int n2, int num) {
//if splice index is greater than the size of 1st
array
if(n1 <= num) {
num = n1;
}
int i = 0;
//declaring memory size for spliced aray
int *spliced = (int *)(malloc(sizeof(int)
*(n1+n2)));
//copying the elements of 1st array upti spliced
index
for(i = 0 ; i < num ; i ++){
*(spliced+i) = (*(arr1+i));
}
//copying the 2nd array
for(int j = 0; j < n2 ; j ++, i++) {
*(spliced+i) = *(arr2+j);
}
//copying the remaining elements of the 1st
array
for(int j = num ; j < n1; j++, i++) {
*(spliced+i) = *(arr1+j);
}
return spliced;
}
//function to populate array
void fillArray(int *arr, int n) {
for(int i = 0 ; i < n ; i ++){
*(arr+i) = 1 + rand() % 101;
}
}
//funcitom to print array
void printArray(int *arr, int n) {
int count = 0;
for(int i = 0 ; i < n ; i++){
cout << *(arr+i) <<
"\t";
count++;
if(count == 10) {
count = 0;
cout <<
endl;
}
}
}
//function to read input, and accept only valid integers
int readInput() {
int n;
while(!(cin >> n)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter an Integer Try again:
";
}
if(n < 0){
cout << "Invalid input. Please enter an positive
Integer Try again: ";
return readInput();
}
return n;
}
int main() {
srand(100);
int n1, n2, num;
//reading user input
printf("Enter size of 1st array: ");
n1 = readInput();
printf("Enter size of 2nd array: ");
n2 = readInput();
printf("Enter the number of elements of the first
array to be copied before the splice: ");
num = readInput();
//declaring and populating 1st array
int *arr1 = (int *)(malloc(sizeof(int)*n1));
fillArray(arr1, n1);
//declaring and populating 2st array
int *arr2 = (int *)(malloc(sizeof(int)*n2));
fillArray(arr2, n2);
printf("Content of Array 1: \n");
printArray(arr1, n1);
printf("\n\nContent of Array 2: \n");
printArray(arr2, n2);
int *spliced = splice(arr1, arr2, n1, n2, num);
printf("\n\nContent of Spliced Array: \n");
printArray(spliced, n1+n2);
}
IF THERE IS ANYTING THAT YOU DO NOT UNDERSTAND, OR NEED MORE HELP THEN PLEASE MENTION IT IN THE COMMENTS SECTION,