I'm attempting to write a recursive program that adds up the sum of a list of arrays. It requires use of a helper function, as reflected below.
I am failing to see what I am doing wrong, but I think it has to do with a failure to get the program to remove items from the list after they are added up. Could anyone help me?
EDIT: Let me explain my logic in pseudocode: I want to get the program to recognize an item in an array list and store that in double sum. Then through recursive passing I want to keep repeating this process by moving through the items in the list of arrays and add them to double sum.
public static double sum (double[] a) {
double sum = 0;
return sumHelper (a, a.length-1, sum); // TODO
}
public static double sumHelper (double[] a, int i, double sum)
{
if (a.length == 0) {
return sumHelper (a, a-1, sum);
}
else {
sum = sum + a[i];
i = i-1;
a.length -1; // THIS LINE DOESN'T WORK
return sumHelper (a, a-1, sum);
}
return sum;
}
public static void main (String[] args) {
double[] list0 = new double[] {};
double[] list1 = new double[] { 5 };
double[] list2 = new double[] { -3, 5 };
double[] list3 = new double[] { 2, -3, 5 };
double[] list4 = new double[] { -1, 2, -3, 5 };
double[] list5 = new double[] { 33, 44, 55 };
System.out.println("Display the sum of the array contents");
System.out.println ("list5: " +sum (list5));
System.out.println ("list0: " +sum (list0));
System.out.println ("list1: " +sum (list1));
System.out.println ("list2: " +sum (list2));
System.out.println ("list3: " +sum (list3));
System.out.println ("list4: " +sum (list4));
a.length -1; // THIS LINE DOESN'T WORKOf course it doesn't. This syntax is invalid.