Using java recursion, no loops or scanners......Merge two arrays into one. Write
ID: 3686835 • Letter: U
Question
Using java recursion, no loops or scanners......Merge two arrays into one. Write a method named merge which takes 3 interger array parameters: a, b, and soln. soln is guaranteed to be the size of a.length + b.length. The arrays a and b should be "merged" into one array by alternating elements. Add a 4th parameter, which indicates which element in soln should be updated for a given recursive call of the method.
Examples:
merge({1, 2, 3}, {4, 5, 6}, soln, 0) should end with the following values in soln: {1, 4, 2, 5, 3, 6}
merge({1,2}, {3,4,5}, soln, 0) should end with the following values in soln" {1,3,2,4,5}
Explanation / Answer
public static void merge(int a[], int b[], int c[], int i)
{
int j=i;
int k;
if(i<=c.length)
{
k=i/2;
if(k<a.length)
{
c[j]=a[k];
j++;
}
if(k<b.length)
{
c[j]=b[k];
j++;
}
merge(a,b,c,i+2);
}
else return ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.