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!!!
#include<iostream>
usingnamespace 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");
}
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);
}
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.
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.
#include <iostream>
usingnamespace 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;
}
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...