user10497264
user10497264

Reputation:

How to recursively add numbers of a random array?

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);
    //}
}

Upvotes: 0

Views: 478

Answers (5)

Richard
Richard

Reputation: 90

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]));
    }
}

}

Upvotes: 0

John Stringer
John Stringer

Reputation: 587

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]);
}

Upvotes: 1

GhostCat
GhostCat

Reputation: 140427

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.

Upvotes: 1

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

Upvotes: 0

Thiyagu
Thiyagu

Reputation: 17890

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.

Upvotes: 4

Related Questions