compound interests-RECURSIVE FUNCTION!!!

Hello!
month intersts compound rate ar=12%
r is monthly compound rate 1/100

If The sum I borrow from bank is 1000
how to make functin result returning the result amouth how much I owe the bank after 2 or more month?
Many thansk!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include<iostream>
using namespace std;


int result(int s, int om, int m){
    int r;
    
    if (m==1)
    c=z*(1+mr);
    else
    r=result(r, om, mn-1);
    
    return c;
}

int main(){
    int z=1000;
    int mr=1/100;
    int m=2;
    cout<<"Result is: "<<result(z, om, m)<<endl<<endl;
    system("PAUSE");
}
    
Last edited on
One very important thing that you should learn from the beginning is to give meaningful names to your variables. Instead of z, om, m etc if they were named rate, principal, years, it would be much easier to debug your code.

That said, first change the types of all the variables to double instead of int.

int mr=1/100; // mr = 0 and not 0.01.

Instead
double mr=1/100;

Change your result function to:
1
2
3
4
5
6
7
8
9
10
double amount( double principal, double rate, double years ){

    // base case. If period == 0 then return without any additional interest.
    if ( years <= 0 )
      return principal;

    // If control has come here, then update principal after one year and call
    // amount function recursively.
    return ( amount( principal*(1+rate), rate, years-1);
}
Last edited on
closed account (j3Rz8vqX)
I do not believe your code, the below, will compile - too many undeclared variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//From main: s==1000, om==unknown, m==2;
int result(int s, int om, int m){
    int r;
    
    if (m==1)//m==2; this will not occur the first time
    c=z*(1+mr);//(mr(what is mr?) +1) * z(what is z?) = c(where is c declared?)
    else
    r=result(r, om, mn-1); //what is om? Not declared in function or main.
    
    return c;
}

int main(){
    int z=1000;
    int mr=1/100;
    int m=2;
    cout<<"Result is: "<<result(z, om, m)<<endl<<endl;//Was "om" declared?
    system("PAUSE");
}

A monthly compound may look somewhat like this:
1
2
3
4
5
6
7
8
9
10
11
//Use a double return type, integer will not retain decimals!
double result(double borrowed_money, double monthly_interest, int number_of_months){
    
    borrowed_money = borrowed_money + (borrowed_money*monthly_interest); //Incremented interest for this month
    if (number_of_months>1) //Does what it says
    {
        borrowed_money=result(borrowed_money, monthly_interest, number_of_months-1); //call this function again with one less month.
        //Assigns "borrowed_money" the returning value.
    }
    return borrowed_money;//returns the value of THIS borrowed_money.
}


There are better ways in which this could be implemented, simply using a while loop may be better, but for consistency purposes I've implemented the process with a recursive function; as to compliment your original design.

Hello!
THANKS ALL!!!

Dput, do U think that in that case value from borrowed_money would change from that value we TOOK from bank to that value WE HAVE TO RETURN???

Many thanks!!!

(p.s. SORRY, I typed mr instead of om in my code, that made the confusion!)
Last edited on
closed account (j3Rz8vqX)
I'm not entirely sure what you're asking; I just couldn't interpret what you said.

But if you're stating whether or not the borrowed_money from the heap would have an effect on the final return, then yes; ideally - I wrote the code on the browser btw.

As each function is called - within the function, it will issue the new borrowed_money as a parameter, the monthly_interest, and the number_of_months decremented by 1.

As each function is collapsed, it will return a double value. It is then assigned to the previous borrowed_money and again returned - collapsing. The process repeats until all of the functions on call are released from memory. The final return would return the value to your printout function.

If your question differs, you may want to ask again; I may not have interpreted correctly.

My example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

double result(double borrowed_money, double monthly_interest, int number_of_months){

    borrowed_money = borrowed_money + (borrowed_money*monthly_interest); //Incremented interest for this month
    if (number_of_months>1) //Does what it says
    {
        borrowed_money=result(borrowed_money, monthly_interest, number_of_months-1); //call this function again with one less month.
        //Assigns "borrowed_money" the returning value.
    }
    return borrowed_money;//returns the value of THIS borrowed_money.
}
int main(){
    double borrowed_money=100,monthly_interest=0.02;
    int number_of_months = 5;
    cout<<"The result of "<<borrowed_money<<" borrowed over "<<number_of_months<<" months, with an interest of "<<monthly_interest*100<<"% is: "<< result(borrowed_money,monthly_interest,number_of_months)<<endl;

return 0;
}

Try it.
Hello!
SORRY, still one unclear thing:

if f.e. number_of_month=3,

then:

b-m(3)=b_m(2)+b_m(2)*m_r;

ok,

but

how does program CONNECT b_m(2) with n-o-m=3?

I hope U understood the question.

(seems just "too authomatised", I woudl just like to dessasemble it to obvious steps)
closed account (j3Rz8vqX)
Ah, the number of months is used to control the number of heaps created by the function.

As long as month is greater than 1, the function would call itself again with the update parameters.

how does program CONNECT b_m(2) with n-o-m=3?

As I stated above, it doesn't directly associate with number of months.
In my design I used number of months to control how many times the borrowed money is incremented.


102 = 100 + (100 * 0.02);

If month is greater than 1, repeat process with updated borrowed money
result(102, 0.02, 5-1)


The number of months is relevant here:
1
2
if (number_of_months>1) //Does what it says
    //If true test again with updated values as shown in the code 
hello!
Yes, i thought it shoudl be like that, sure, but it looks "the "loop" is more to feel then to follow by our thoughts,,,"...to say sth like that...
closed account (j3Rz8vqX)
That's exactly what it is. We are technically making a loop with heaps of the function - recursion.

http://www.cplusplus.com/forum/beginner/121645/#msg663068

A function is just extended code, and what we've done is added a comparison (the if condition) and recalled the function; hence being a loop.

There's no one way to do something.

Example:
1
2
3
4
5
while(number_of_months>1)
{
    borrowed_money = borrowed_money + (borrowed_money*monthly_interest); //Incremented interest for a month
    number_of_months--;
}
Topic archived. No new replies allowed.