views:

170

answers:

4
+1  Q: 

recursive program

I am trying to make a recursive program that calculates interest per year.It prompts the user for the startup amount (1000), the interest rate (10%)and number of years(1).(in brackets are samples)

Manually I realised that the interest comes from the formula YT(1 + R)----- interest for the first year which is 1100.

2nd year YT(1 + R/2 + R2/2) //R squared

2nd year YT(1 + R/3 + R2/3 + 3R3/) // R cubed

How do I write a recursive program that will calculate the interest? Below is the function which I tried

//Latest after editing

double calculateInterest2(double start, double rate, int duration) 
{ 
    if (0 == duration) { 
        return start; 
    } else { 
        return (1+rate) * calculateInterest2(start, rate, duration - 1); 
    } 
} 
+1  A: 

You forgot to decrement duration, so you end up with an infinite loop (or more likely in case of a recursion, a stack overflow).
You also do nothing with the result of calculateInterest, so you're returning the value for the first year:

return calculateInterest(cpdInterest, rate, duration - 1);

Also, you may want to change duration to an int, or handle the case 0 < duration < 1.

Kobi
+1  A: 

Apart from not decrementing the duration (which leads to infinite recursion), it looks like you're not doing anything with the data you get back from your recursive calls. The key to writing recursive functions is in knowing when to stop, and knowing what to do with the data you get back from each call.

In this case, you want to stop when the number of years is 0. If you keep money in the bank for 0 years, you will get no interest, so in the 0 case, you want to return 0.

For durations greater than 0, since you want to calculate the total interest you accumulate over the years, you should add the interest from the current year with the interest from the remaining years (in which the startUp amount will also include the interest from the current and past years). So you'd have something like

return (startUp*rate) + calculateInterest(startUp*(1+rate), rate, duration-1);
mandaleeka
+6  A: 

I took the liberty of testing your function in Java (the syntax is similar), and it returned weird results. Here is what I got:

calculateInterest2(1000, .1, 0); // = 1000.0
calculateInterest2(1000, .1, 1); // = 1200.0
calculateInterest2(1000, .1, 2); // = 1420.0
calculateInterest2(1000, .1, 3); // = 1662.0
calculateInterest2(1000, .1, 4); // = 1928.2

Obviously, this is not right. First of all, the return line is also re-applying the calculation.... Here is a rewrite of the method:

static private double calculateInterest2(double start, double rate, int duration)
{
    if (0 == duration) {
        return start;
    } else {
        return (1+rate) * calculateInterest2(start, rate, duration - 1);
    }
}

As you can see, this method checks out with this output :

calculateInterest2(1000, .1, 0); // = 1000.0
calculateInterest2(1000, .1, 1); // = 1100.0
calculateInterest2(1000, .1, 2); // = 1210.0
calculateInterest2(1000, .1, 3); // = 1331.0
calculateInterest2(1000, .1, 4); // = 1464.1000000000001

Sounds more right to me.

Yanick Rochon
+1 for being new! Welcome. I don't quite see the point in posting the first version, with the error you've introduced. The Original Poster (OP) returns `startUp` when `duration == 0`, so the issue didn't exist there.
Kobi
yes, the OP modified his question as answers were posted... cleaning up my post now...
Yanick Rochon
thanks so much for the solution.
wilson88
The OP definitely modifies his question as he goes along; he edited his question about 4-5 times and the initial method is not there anymore... anyway. I'm done here :) @wilson88, Please mark this as resolved if this solution is what you want.
Yanick Rochon
+2  A: 

Basically, a recursive function F works by calculating the answer for F(N) off of the answer for F(N-1) plus the extra work for the Nth case. And of course you have to supply the terminating result for N==0 (or 1 or whatever)

So in your case, the compound interest for year N would be:

  Interest(S,R,N) = (S + Interest(S,R,N-1)) * R

where

  Interest(S,R,0) = S * R

And you should be able to change your code to represent that pretty easily.

quarter