views:

322

answers:

5

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

}

}

+4  A: 

There are several things wrong with your code. I suspect that you simply don't understand what you're trying to do. That can't be fixed here -- you need to spend some more time studying until you really get it.

But here's a few things I noticed that may point you in the right direction:

  • What is the purpose of the n argument? Should it be somehow related to the length of the array? Perhaps you could use method overloading to pass it to your recursive function but not to the recursion entry point?

  • Why do you have a return 1; in your code? What is the 1 supposed to represent?

  • You have declared a variable sum, but it is never used. Where were you intending to use the sum variable?

Perhaps by thinking about these questions you will be able to find a working solution.

Daniel Pryden
Better than doing his homework for him. I will try to delete my answer
Brian Fisher
@Brian F - yes. I agree with your comments re. obvious homework questions
Brian Agnew
+1  A: 

Without giving the answer away (as this is homework!), I would advise separating the recursion mechanism from the addition mechanism. That is to say, your recursion looks good, but you only need to sum the negative numbers, and ignore the positive/0 ones. That should give you enough to solve the problem!

Brian Agnew
+1  A: 

The problem is when the array contains a positive number, the recursion stops and returns a 1. That means that it will abort at the first positive number (going backwards) in the list. Is that what you want?

A couple of other questions, you don't start at the last position in the array, why?

In a recursion algorithm, you have to check when you are done, you currently define being done as hitting the first positive number. After you fix that, you have to have a different check for being done.

Yishai
A: 

I agree with previous authors, there is a wrong in your code, you can't get -33 when you have mix of negative and positive numbers in array. So removing the positive numbers, just adding the only negative numbers, using tail-recursive algorithm, we can easily find the value -33. I won't recommend using recursion for this type of solution, as it is easily possible with iteration using for..loop or other loop method. Recursion is useful but not in all cases. It is useful to find factorial, fibonacci numbers, even for binary search and sorting. Anyways, I presented my solution, hope it will help you.

public class NegSum {
public static void main(String []args)
{
    int array[]={-7,-19,-6,-1};

    RecursionMethod obj=new RecursionMethod();

    System.out.println(obj.findSum(array, 1));
} 

public class RecursionMethod {
int sum=0;

public int findSum(int array[], int n)
{   
    if (array.length==n-1) return sum;
    else
        sum= sum+ array [n-1];
        n++;

/* here it is tail-recursive algorithm instead of pure recursion; in pure recursion, computation is carried out after recursive call but in tail-recursive algorithm , calculation is carried out before calling the recursive method. */

       return findSum(array, n);         
}

}

Zakir Sajib
A: 

You should put System.out.println(obj.findSum(array, 1)); or debug the program. Then you will understand how it works. What was your thinking behind if(array[n]>0) return 1;?

fastcodejava
or use `Jeliot` to see what is happening: http://cs.joensuu.fi/jeliot/
Carlos Heuberger