Question

In: Computer Science

Write a function called splice() that “splices” two integer arrays together by first allocating memory for...

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.

Solutions

Expert Solution

#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,


Related Solutions

Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
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
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT