Question: Write a recursive function called sumArray() that determines the sum of the integers in an array A[0...n-1]. Do this in 3 ways. a. Recur on
Write a recursive function called sumArray() that determines the sum of the integers in an array A[0...n-1]. Do this in 3 ways. a. Recur on A[0...n-2], add the result to A[n-1], then return the sum. b. Recur on A[1...n-1], add the result to A[0], then return the sum. c. Split A[0...n-1] into two subarrays of length (approximately) n/2, recur on the two subarrays, add the results and return the sum. Hint: think about MergeSort(). 4. Write a modification of the recursive function BinarySearch() that prints out the sequence of array elements that are compared to the target. 5. What output does the following program produce? public class problem5 { public static int getValue(int a, int b, int n){ int x, c; System.out.println("arrive: a = " + a + " b = " + b); c = (a+b)/2; if( c*c <= n ){ x = c; }else{ x = getValue(a, c-1, n); } System.out.println("depart: a = " + a + " b = " + b); return x; } public static void main(String[] args){ System.out.println(getValue(3, 13, 5)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
