// Hunter Schafer, CSE 143 // Simple program that demonstrates some recursive solutions to problems public class Recursion2 { public static void main(String[] args) { int[] array = {1, 2, 3}; System.out.println("sum of array: " + sum(array)); } // returns the sum of all the elements in the array. // 0 if the array is empty. public static int sum(int[] array) { return sum(array, 0); } // returns the sum of all the elements in the array starting // at given index. Returns 0 if index is past the end of the array. private static int sum(int[] array, int index) { if (array.length >= index) { return 0; } else { // array[0] + sum (array starting at index 1) // array[1] + sum (array starting at index 2) == 3 // array[2] + sum(array starting at index 3) return array[index] + sum(array, index + 1); } } }