C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 24th, 2005, 12:41 PM
jamie1988 jamie1988 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 4 jamie1988 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 5 sec
Reputation Power: 0
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");

}

Reply With Quote
  #2  
Old November 24th, 2005, 02:08 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 37 m 46 sec
Reputation Power: 4
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.

Reply With Quote
  #3  
Old November 25th, 2005, 12:10 PM
jamie1988 jamie1988 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 4 jamie1988 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 5 sec
Reputation Power: 0
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++;
}

Reply With Quote
  #4  
Old November 26th, 2005, 05:30 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 37 m 46 sec
Reputation Power: 4
Ok here is _An_ example of a recursive function which does something like that. It should be clear I think.

cpp Code:
Original - cpp Code
  1. #include <stdlib.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int TARGET = 1000000;
  7.  
  8. float investmentOneYearLater(float investment, int interestRate)
  9. {
  10.         return investment*(1.0+(interestRate/100.0));
  11. }
  12.  
  13. int calculateRec(float investment, int interestRate)
  14. {
  15.         // STOP CONDITION FIRST
  16.         if( investment >= TARGET )
  17.         {
  18.                 return 0;
  19.         }
  20.         else
  21.         {
  22.                 // RECURSIVE STEP
  23.                 return 1 + calculateRec(
  24.                         investmentOneYearLater(investment, interestRate),
  25.                         interestRate);
  26.         }
  27. }
  28.  
  29. int main()
  30. {
  31.         float initialInvestment;
  32.         int interestRate;
  33.         cout << "Initial investment: ";
  34.         cin >> initialInvestment;
  35.         cout << "Interest rate: ";
  36.         cin >> interestRate;
  37.  
  38.         cout << "It will take " << calculateRec(initialInvestment, interestRate)
  39.                          << " years to reach a million." << endl;
  40.  
  41.         return 0;
  42. }
  43.  

Reply With Quote
  #5  
Old November 26th, 2005, 05:41 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 37 m 46 sec
Reputation Power: 4
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.

Reply With Quote
  #6  
Old November 29th, 2005, 05:31 PM
jamie1988 jamie1988 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 4 jamie1988 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 5 sec
Reputation Power: 0
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.

Reply With Quote
  #7  
Old November 30th, 2005, 02:21 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 37 m 46 sec
Reputation Power: 4
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..).

Reply With Quote
  #8  
Old November 30th, 2005, 01:56 PM
jamie1988 jamie1988 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 4 jamie1988 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 5 sec
Reputation Power: 0
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.

Reply With Quote
  #9  
Old November 30th, 2005, 02:32 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 37 m 46 sec
Reputation Power: 4
Post

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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |