开发者

recursive program

开发者 https://www.devze.com 2023-01-01 00:09 出处:网络
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)

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 calculateInteres开发者_如何学运维t2(double start, double rate, int duration) 
{ 
    if (0 == duration) { 
        return start; 
    } else { 
        return (1+rate) * calculateInterest2(start, rate, duration - 1); 
    } 
} 


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.


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.


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.


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


I have made this program using functions in C

//include standard libraries

#include<stdio.h>

//user defined function

float cmp_i(float prin,float rate,int time);

//start of main function

main()
{

        //declaration of variables

        float prin,rate,ci;
        int time;

        //input by the user

        printf("Please enter the principle ammount\n");
        scanf("%f", &prin);
        printf("Please enter thr rate of interest\n");
        scanf("%f",&rate);
        printf("Please enter the time in years");
        scanf("%d", &time);

        //calculation and result display

        ci=cmp_i(prin,rate,time);
        printf("The compound interest for the given ammount is %f",ci);


}//end of main function
//start of cmp_i function


float cmp_i(float prin,float rate,int time)
{

    //declaration of variables
    int i;
    float intr=0;
    intr=(prin*rate)/100;
    for(i=01;i<=time;i++)
    {
        intr=intr+(intr*rate)/100;
    }
    return(intr);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消