0

Hi I'm just learning recursion and I am trying to write a recursive program in Java to find the sum of all positive elements in an array. Using : here are the expected outputs:
0 5 7 3 0 17

import java.util.Arrays;
public static void main(String[] args) {
    int[] list0 = new int[] {};
    int[] list1 = new int[] { 5 };
    int[] list2 = new int[] { 3, 4 };
    int[] list3 = new int[] { -2, 3, -4 };
    int[] list4 = new int[] { -1, -2, -4, -5 };
    int[] list5 = new int[] { 6, 1, 2, -3, 8 };
}
public static int sumOfPositivesRecursive (int[] a) {
    return sumOfPositivesHelper(a, a.length);
}

public static int sumOfPositivesHelper(int[] a, int n) {
    if(n == 0) {
        return 0;
    }
    System.out.println(n);
    int total = 0;
    if(a[n-1] > 0) {
        total += a[n-1];
        sumOfPositivesHelper(a, n-1);
    }
    return total;
}

My output: 0 5 4 0 0 8, only seems to check the last element the first time and never loops through again. Please help I know I am doing something wrong with the recursive call. Thanks all.

1
  • Your sumOfPositivesHelper should call itself, which it is not doing at the moment. Commented Feb 8, 2020 at 19:57

3 Answers 3

2
import java.util.Arrays;

public class HelloWorld{

public static void main(String[] args) {
    int[] list0 = new int[] {};
    int[] list1 = new int[] { 5 };
    int[] list2 = new int[] { 3, 4 };
    int[] list3 = new int[] { -2, 3, -4 };
    int[] list4 = new int[] { -1, -2, -4, -5 };
    int[] list5 = new int[] { 6, 1, 2, -3, 8 };

    int sum = sumOfPositivesRecursive(list5);
    System.out.println("the sum of all positive number is " + sum);
}

public static int sumOfPositivesRecursive (int[] a) {
    int i =0;
    return sumOfPositivesHelper(a, i, 0);
}

public static int sumOfPositivesHelper(int[] a, int i, int sum) {
    if(i == a.length) {
        return sum;
    }
    if(a[i] > 0) {
        sum+= a[i];
        return sumOfPositivesHelper(a, ++i, sum);
    }else{
        return sumOfPositivesHelper(a, ++i, sum);
    }
  }
}

Just made it work but the concept remain the same, were just missing a few returns ;)

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

Comments

1

You code doesn't use recursion properly as the total variable is not passed between the sumOfPositivesHelper calls and not handled correctly, a valid solution will be something like that:

package test;

public class Main {

    public static void main(String[] args) {
        System.out.println(sumOfPositives(new int[] {}));
        System.out.println(sumOfPositives(new int[] { 5 }));
        System.out.println(sumOfPositives(new int[] { 3, 4 }));
        System.out.println(sumOfPositives(new int[] { -2, 3, -4 }));
        System.out.println(sumOfPositives(new int[] { -1, -2, -4, -5 }));
        System.out.println(sumOfPositives(new int[] { 6, 1, 2, -3, 8 }));
    }

    private static int sumOfPositives(int[] list) {
        return list.length == 0 ? 0 : sumOfPositives(list, 0, 0);
    }

    private static int sumOfPositives(int[] list, int index, int total) {
        if (list[index] > 0) {
            total++;
        }
        if (index + 1 < list.length) {
            return sumOfPositives(list, index + 1, total);
        }
        return total;
    }

}

1 Comment

thank you for this insight. makes a lot more sense now.
1

Do it as follows:

public class Main {
    public static void main(String[] args) {
        int[] list0 = new int[] {};
        int[] list1 = new int[] { 5 };
        int[] list2 = new int[] { 3, 4 };
        int[] list3 = new int[] { -2, 3, -4 };
        int[] list4 = new int[] { -1, -2, -4, -5 };
        int[] list5 = new int[] { 6, 1, 2, -3, 8 };

        System.out.println("Sum of positive elements in list0: " + sumOfPositivesRecursive(list0));
        System.out.println("Sum of positive elements in list1: " + sumOfPositivesRecursive(list1));
        System.out.println("Sum of positive elements in list2: " + sumOfPositivesRecursive(list2));
        System.out.println("Sum of positive elements in list3: " + sumOfPositivesRecursive(list3));
        System.out.println("Sum of positive elements in list4: " + sumOfPositivesRecursive(list4));
        System.out.println("Sum of positive elements in list5: " + sumOfPositivesRecursive(list5));
    }

    public static int sumOfPositivesRecursive(int[] a) {
        return sumOfPositivesHelper(a, a.length);
    }

    public static int sumOfPositivesHelper(int[] a, int n) {
        if (n == 0) {
            return 0;
        }
        return a[n - 1] > 0 ? a[n - 1] + sumOfPositivesHelper(a, n - 1) : sumOfPositivesHelper(a, n - 1);
    }
}

Output:

Sum of positive elements in list0: 0
Sum of positive elements in list1: 5
Sum of positive elements in list2: 7
Sum of positive elements in list3: 3
Sum of positive elements in list4: 0
Sum of positive elements in list5: 17

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.