0

Given the array

static int[] testArray = {8, -5, 22, 17, 4, 9, -12, 15, 23, 25};'

How can I recursively add all the values together. I have tried the below code which of course does not work. As it should and is not very logical as i would not know how to call it

static int[] testArray = {8, -5, 22, 17, 4, 9, -12, 15, 23, 25};

static int i = 0;
public static void reverse(int sum) {
    i = i+1;
    int sumNum = sum;

    //if ((n/10) != 0) {
    sumNum = sumNum + testArray[i];
    reverse(sum);
    //}
}
4
  • 6
    Why is a method that should add numbers called reverse? Commented Dec 18, 2018 at 13:25
  • 3
    this looks really strange. Why do you want to do it by recursion? A simple loop would be easyer to implement and also more efficient Commented Dec 18, 2018 at 13:26
  • why do you want it to be recursive? Is that a must? Commented Dec 18, 2018 at 13:27
  • 2
    The sum of the array is the sum of its first element and the sum of the rest of the array. Commented Dec 18, 2018 at 13:28

5 Answers 5

4
static int sumRecursive(int index) {
    if (index == testArray.length) {
        return 0;
    }
    return sumRecursive(index + 1) + testArray[index];
}

We pass the index as a parameter to the function. On each call, we send the value of the current index + 1. When we reach the end, we return 0 (the base case). The result is added to the current element and returned.

The initial call is made as sumRecursive(0).

If the original array is not static, then you would have to pass the array as a parameter as well.

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

4 Comments

but this always returns 0, no?
@Katie.Sun See + testArray[index]
if index == length of array, return 0 ?
@Katie.Sun Yes. It it equivalent to index == arr.length - 1 then return arr[index]
1

More of an addendum, as this is probably some homework, and you already got the straight forward answer (by passing down an index to your recursive method).

Of course, the real fun here is to think functional!

Meaning: instead of around the complete array, or an index within a "global" array, you pass around "lists". And then fold results together, as outlined here.

Long story short: the non-trivial solution does something like:

int sum(List<Integer> values) {
  if (values.size) == 0 return 0;
  return values.get(0) + sum(values.subList(1, values.size());

Of course, Java isn't really suited for such kind programming, as creating these sublists isn't exactly a good fit for this simple problem.

Comments

1

Why are we doing this recursively? If there's no good reason for recursion then keep things simple, the following single line will sum your array:

Arrays.stream(testArray).sum();

If however this is some test questing forcing you to use recursion then the other answers already provided are I guess the best option, i.e. something like:

public static int recursiveSum(int[] array, int index, int sum) {
    return index == array.length ? sum : recursiveSum(array, index + 1, sum + array[index]);
}

All together with a main method that looks like:

static int[] testArray = { 8, -5, 22, 17, 4, 9, -12, 15, 23, 25 };

public static void main(String[] args) {
    System.out.println(Arrays.stream(testArray).sum());
    System.out.println(recursiveSum(testArray, 0, 0));
}

public static int recursiveSum(int[] array, int index, int sum) {
    return index == array.length ? sum : recursiveSum(array, index + 1, sum + array[index]);
}

Comments

0

If you REALLY need it to be solved recursively, one thing you can do is have the recursive method (let's call it sumRecursive(array)) have 1 parameter containing the rest of the array.

If you have an array: [1, 2, 3, 4]. You call sumRecursive([1, 2, 3, 4]) which calls sum with sumRecursive([2, 3, 4]) and the return value is array[0] + *rest of the array*.


However, I really not recommend a simple summing to be recursive, it's inefficient and more complicated

Comments

0
public class Recursion {

static int[] testArray = { 8, -5, 22, 17, 4, 9, -12, 15, 23, 25 };
static int i = 0;
static int sumNum = 0;

public static int reverse(int sum) {
    i++;
    sumNum = sumNum + sum;

    if (i < testArray.length) {
        reverse(testArray[i]);
    }
    return sumNum;
}

public static void main(String[] args) {

    if (testArray.length != 0) {
        System.out.println("Sum:" + Recursion.reverse(testArray[i]));
    }
}

}

Comments

Your Answer

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