How to write the recursion code in calculating the sum of the array elements at odd value?
I understand the factorial example using recursion, but I am having a really big trouble when writing the recursion codes to perform anything else on my own :/ Please help with this task that I am stuck on.
I need to calculate the sum of the array elements at odd value using recursion.
I need to write the codes in:
public static double oddSum(double[] list, int sIndex, int eIndex) {
}
list is my double arrayList
sIndex is the initial index
eIndex is the end index
My idea is that my stopping point will be sIndex = eIndex, which would mean that I have checked every element on the list. I write the following code with confusion :/ I decided to paste it here to show that I am trying (even though the code is incorrect).
public static double oddSum(double[] numbers, int sIndex, int eIndex){
int sum = 0;
if(sIndex == eIndex)
return sum;
else
if(sIndex+1%2 != 0)
sum += numbers[sIndex+1];
return sum;
}
Please correct me if I have misunderstood. Thank you very much in advance.