Write a complete recursive method called arraySum that given

Write a complete recursive method called arraySum that given an array of ints, returns the sum of all values in the array. Start represnts where the call will start within the array. The initial call will pass 0 for start. Describe the base case and the general case giving both the condition in which it occurs and action peformed.

Solution

public static int Sum(int[] a, int start) {
  
if(start>=a.length)
return 0;
  
return a[start]+Sum(a,start+1);
}

Base Case;

If start index is equal to or greater than the number of element in array return 0

else

add the current element to the previous sum and call for next index.

Write a complete recursive method called arraySum that given an array of ints, returns the sum of all values in the array. Start represnts where the call will s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site