| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C++ help
i use Dev-C++.... and i have a problem with my coding of recursions... here is the code
#include <stdlib.h> #include <iostream> using namespace std; int bal, inst, instr8; void million (int time){ million (time+1); } int main() { if (bal<1000000){ inst=(bal*instr8)/100; bal=inst+bal; million (1); } else { cout<<"Please enter your desired deposit value! \n"; cin>>bal; cout<<"Please enter your desired interest value per year! \n"; cin>>instr8; cout<<"You will make "<<bal<<" in "<<time<<" years \n"; } cin.get(); system ("Pause"); } |
|
#2
|
||||
|
||||
|
The first thing you should think about when writing a recursive function is its stop condition. You don't have one...
![]() p.s. you might want to look at the variable/function(?) time you have in the last output statement... It probably does not do what you think it does. |
|
#3
|
|||
|
|||
|
thnx...
this is the assignment i had to do:
"Write a simple program that uses recursion to take in a user’s input for initial investment and rate of interest then prints out the number of years it takes for that investment to reach 1 million dollars." i used the equation from a previous assignmet that was exacly the same but without the recursion. time=0; while (bal<1000000){ inst=(bal*instr8)/100; bal=inst+bal; time++; } |
|
#4
|
|||||
|
|||||
|
Ok here is _An_ example of a recursive function which does something like that. It should be clear I think.
cpp Code:
|
|
#5
|
||||
|
||||
|
Offcourse this piece of code needs some extra checks on the input values and such (what happens when people enter negative numbers?). It's just an example of how recursion works, the rest is up to you.
|
|
#6
|
|||
|
|||
|
Thankyou
thanx for the help... no one at my school knows alot about C++. Plus its highschool, and most of the colleges/univercities around here assume we have no coding background.
|
|
#7
|
||||
|
||||
|
You're welcome! Recursion is one of the more difficult concepts to learn but once you understand it you can see how elegant and powerful it is (although there's always a potential stack overflow..).
|
|
#8
|
|||
|
|||
|
I am ahving some trouble understanding a few lines of code... and i don't understand that much about functions, and why they are used but oh well.
Code:
return 1 + calculateRec(
investmentOneYearLater(investment, interestRate),
interestRate);
i don't understand all the function stuff, and why there are so many variables. Code:
calculateRec(initialInvestment, interestRate) i don't understand how investment, was swaped for initialInvestment, when initialInvestment hasn't been used pretty much anywhere except when it was defined, and recieved a value. |
|
#9
|
||||
|
||||
|
Ok let's go through it with an example, let's say you want to know how long it takes to get to a million if you already have 400.000 euro (or dollars or lollypops
) and the interest rate is a whopping 50%!If you start with 400.000 then a year later you have: 400.000 * 1.5 = 600.000 euro, if that's not clear, it's done like: 400.000 * (1 + (50/100)) After another year (year 2) you have 600.000 * 1.5 = 900.000 euro and after three years you have more than a million: 900.000 * 1.5 = 1.350.000 Offcourse you already knew all this, the code does a very similar thing. So when calculateRec is called for the first time, it is called with the parameters calculateRec(400000, 25). When we enter the function calculateRec itself this means that its parameters investment and interestRate are set to 400000 and 25 respectively. Since investment is not yet a million it will go into the else and call calculateRec again, but now for the investment parameter we have: investmentOneYearLater(investment, interestRate) and the interestRate parameter stays the same. investmentOneYearLater(investment, interestRate) does nothing more then calculate interest (well total amount+interest) after one year based on investment and interestRate. It is the same mathematics as shown above. This will then return 600.000 for our example. So when calculateRec is called a second time it is called with parameters 600.000 and 25. Now the same story continues, 600.000 is still smaller than a million (TARGET) so we will yet again call calculateRec, but now with yet again the amount+interest after one year. One year after your lump of money has become 600.000 that is.. So we end up with 900.000. Rinse and repeat again and we call calculateRec with 1.350.000 as investment, calculateRec will now return 0. You have to read this as such: how long does it take for me to get to a million if i start with 1.35 million? Answer: 0 years, so there's you're answer to that question. When calculateRec returns 0 it comes back to when we were calling calculateRec with an amount of 900.000. That call returns with the value 1+0 = 1. After that it returns 1+1 = 2 and after that 1+2 = 3 years. This is what is happening, to understand it you can interpret it as follows: calculateRec will return you the number of years it takes to get a million starting with investment. If investment >= million that means 0 years. If it is not a million yet, it means that you will have to wait AT LEAST one year plus the time it takes to get to a million after that one year. So we only need to calculate the time it takes to get to a million after that one year and add 1 for 'this' year, right? No how do we do that? Well we already have a function that does that: calculateRec! So we recursively let that function handle it.It's a rather difficult concept, I hope my explanation makes sense, it's quite hard explaining it without pen and paper or a whiteboard or something.. Hope this helps. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|