Write a function called combineArrays(int[], int, int[], int) that accepts in two arrays of ints and the size of the arrays as parameters. The function should then create a new array that is the size of both array combined. The function should copy the elements from the first array into the new array, followed by all the elements from the second array. So if the initial array is {1,2,3,4,5} and the second arrray is {10,20,30} then the new array will be size 8 and contain {1,2,3,4,5,10,20,30}. Then in your main(), create two arrays of different sizes, each at least size 10, and fill them with integers's. Run the arrays through your function created above and print out all three arrays to demonstrate that the concatenation method worked. Then run your method again using the same two original arrays, but in the other order. So if the first time you did array a then array b, the second time do array b then array a. Make sure to delete the new arrays before closing your program.

Respuesta :

Answer:

The function in C++ is as follows:

void combineArrays(int arr1[], int len1, int arr2[], int len2){

   int* arr = new int[len1+len2];

   for(int i =0;i<len1;i++){

       arr[i] = arr1[i];    }

   for(int i =0;i<len2;i++){

       arr[len1+i] = arr2[i];    }

   for(int i=0;i<len1+len2;i++){

       cout<<arr[i]<<" ";    }    

   delete[] arr;  }

Explanation:

This defines the function

void combineArrays(int arr1[], int len1, int arr2[], int len2){

This creates a new array

   int* arr = new int[len1+len2];

This iterates through the first array.

   for(int i =0;i<len1;i++){

The elements of the first array is entered into the new array

       arr[i] = arr1[i];    }

This iterates through the second array.

   for(int i =0;i<len2;i++){

The elements of the second array is entered into the new array

       arr[len1+i] = arr2[i];    }

This iterates through the new array

   for(int i=0;i<len1+len2;i++){

This prints the new array

       cout<<arr[i]<<" ";    }

This deletes the new array

   delete[] arr;  }

See attachment for complete program which includes the main

Ver imagen MrRoyal