Write a recursive function called sumArray() that determines the sum of the integers in an array A[0...n-1]. Recur on A[0 ... n-2] , add the result to A[n-1] , then return the sum.
Code:
static int sum1(int[] A, int p, int r) {
int r2= r-1;
if (p==r)
return p;
else if(p==r2)
return A[r2]+A[p];
else
p=sum1(A,p+1,r2)+p;
return p+A[r];
}
The array A I'm inputting is int[] A = {1,2,3,4,5,6,7,8,9,10} which leads to a value of 50, not 55. What is wrong with my code?