0

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));
8
  • where is the recursion? Commented Jan 23, 2017 at 18:33
  • You never call sumHelper in the else block of sumHelper so there isn't any recursion happening Commented Jan 23, 2017 at 18:34
  • Sorry forgot to include the recursive lines, which I temporarily removed to test shortening the length of the arrays by other means. It is edited to include them again. Commented Jan 23, 2017 at 18:36
  • 1
    Also you are passing the index to the helper so you don't need to modify the array at all, just update the index and sum Commented Jan 23, 2017 at 18:36
  • a.length -1; // THIS LINE DOESN'T WORK Of course it doesn't. This syntax is invalid. Commented Jan 23, 2017 at 18:39

2 Answers 2

1

Is this is what you are trying to do?

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 (i < 0) { 
        return sum;
    }
    else {
        sum = sum + a[i];
        i = i-1;
        return sumHelper (a, i, 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));

}

Results:

Display the sum of the array contents list5: 132.0 list0: 0.0 list1: 5.0 list2: 2.0 list3: 4.0 list4: 3.0

Sign up to request clarification or add additional context in comments.

2 Comments

That was pretty much it. Clearly I confused my death to death by trying to adjust i both recursively and within main, and then on top of that needlessly trying to adjust the length of a in three different places. This is tremendously helpful! Sorry for the newcomer question!
Anyways Java arrays length are immutable. (You can't change the length).
0

You misplaced the variables, a-1 instead of i. And array will start from 0 index, So you need to add the elements till the 0 index.

static double[] list0 = new double[] {};
        static double[] list1 = new double[] { 5 };
        static double[] list2 = new double[] { -3, 5 };
        static double[] list3 = new double[] { 2, -3, 5 };
        static double[] list4 = new double[] { -1, 2, -3, 5 };
        static double[] list5 = new double[] { 33, 44, 55 };
        public static void main(String[] args) {
            // TOsuDO Auto-generated method stub
            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));
        }
        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 (i == -1) {
                {
                    System.out.println("Added all the elements");
                    return sum;
                }
            }
            else {
                sum = sum + a[i];
                i = i-1;
                //a.length -1; // THIS LINE DOESN'T WORK
                return sumHelper (a, i, sum);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.