0

I understand that finding the sum of numbers in an array is much more easily done through iteration instead of recursion, but if I were to use recursion to write such a function, what would be wrong with this code?

public static double sum (double[] a) {
    if (a.length == 0)
        return 0.0;
    else{
        return sumHelper (a, 1, a[0]);
    }
}
private static double sumHelper (double[] a, int i, double result) {
    if (i < a.length) {
        result = result + sumHelper (a, i + 1, result);
    }
    return result;
}

Everything runs without errors, yet does not return the correct sum when I test it out.

1
  • Whittling the problem down by one on each recursive call will lead to a stack size that's O(n). If there's any chance you'll have to deal with large arrays, you'd do better to write sumHelper to return the sum on a range. If the range has length 1, return that element's value. Otherwise split the range in half, recursively call sumHelper on each half, and return the sum the results. This isn't any less work, but will give O(log n) growth to the stack and allow you to deal with much larger arrays.
    – pjs
    Commented Jan 19, 2017 at 16:11

4 Answers 4

2
public class RecursiveSum {
    public static void main(String[] args) {
        System.out.println(sum(new double[] {1,3,4,5}));
    }

    public static double sum(double[] a) {
        if (a.length == 0)
            return 0.0;
        else{
            return sumHelper(a, 0);
        }
    }

    private static double sumHelper(double[] a, int i) {
        if(a.length - 1 == i){
            return a[i];
        }else{
            return  a[i] + sumHelper(a, i + 1);
        }
    }
}
2
  • 1
    Notice the loop invariant
    – vonzhou
    Commented Jan 18, 2017 at 6:04
  • 1
    Thank you! Exactly what I was looking for. That's a much better way of going about it, excluding the third argument.
    – Brian
    Commented Jan 18, 2017 at 6:14
2

Initialise the value of i to 0 because you are passing 1. or try this one

public static void main(String args[]){
    double a[]=new double[10];
    a[0]=123;
    a[1]=123;
    a[2]=123;
    a[3]=123;
    a[4]=123;
    a[5]=123;
    a[6]=123;
    a[7]=123;
    a[8]=123;
    a[9]=123;
    System.out.println(sum(a));
}
1
  • I already take the first index of the array (0) into account with a[0]. Therefore I want to take that initial index, and add on every index starting after 0. So that's why I initialized it at 1.
    – Brian
    Commented Jan 18, 2017 at 5:54
1

You have problem with main method declaration:

public static void main(String args[]){
        double a[]=new double[10];
        a[0]=123;
        a[1]=123;
        a[2]=123;
        a[3]=123;
        a[4]=123;
        a[5]=123;
        a[6]=123;
        a[7]=123;
        a[8]=123;
        a[9]=123;
        System.out.println(sum(a));
    }
1
  • The code I included is not the entire file. I do have different arrays initialized to use as tests, as well as a main method that's already declared properly. However, the rest of the file was already given, only the snip of code I included was what I added to the file. I am sure the problem lies within my code that I included above. As I said, everything compiles and runs correctly, I'm simply getting a different result than what's expected. For example, when testing it with an array including -3, and 5, the sum would be 2, however it's returning -6.
    – Brian
    Commented Jan 18, 2017 at 5:59
0

If you just use a single method to recursively sum all the numbers in an array, then this is how you would go. public static void main(String[] args) {

      double a[]=new double[6]; //initialize it
        a[0]=1; //fill it with values
        a[1]=2;
        a[2]=3;
        a[3]=4;
        a[4]=5;
        a[5]=6;

        System.out.println(sum(a, a.length-1)); //call the method and print as well

}

public static double sum( double arr[], int n ) { 
      if (n < 0) {
        return 0;
      } else{
        return arr[n] + sum(arr, n-1); //magic 
      }
    }
1
  • Thank you! However I would prefer to keep it as 2 separate methods in this case. I understand condensing it into 1 method would be simpler and more efficient.
    – Brian
    Commented Jan 18, 2017 at 6:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.