Hi I am working on the following problem:
Write a recursive function that calculates the sum of the negative numbers of the array.
Pass in the array {15 , -7 , -19 , 8 , 5 , -6 , -1} from the main method .
The recursive function should return the result -33 . Print out this value in the main method.
This program should be named Negsum.java
This is what I have so far, it prints out 6.0, not -33. Thanks everyone.
public class Negsum{
static double findSum(double array[], int n){
double sum=0;
if(array[n]>0)
return 1;
else
return array[n-1]+findSum(array, n-1);
}
public static void main(String args[]){
double array[]={15, -7, -19, 8, 5, -6, -1};
System.out.println(findSum(array, 5));
}
}