Iteration vs recursion

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 1
Reputation: 26933 is an unknown quantity at this point 
Solved Threads: 0
26933 26933 is offline Offline
Newbie Poster

Iteration vs recursion

 
0
  #1
May 15th, 2008
Hi,everybody
I am a beginner in c++;Could you please tell me how to change an iteration program to a recursive one and vice versa?It will be greatly appreciated if you could show me with the help of a few examples(programs).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,476
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Iteration vs recursion

 
0
  #2
May 15th, 2008
If this is the original function
  1. int foo(int x)
  2. {
  3. while(x < 10)
  4. x = x + 1;
  5. return x;
  6. }

Then you can do this:
  1. int foo(int x)
  2. {
  3. if(x < 10)
  4. {
  5. x = x + 1;
  6. foo(x);
  7. }
  8. return x;
  9. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Iteration vs recursion

 
0
  #3
May 15th, 2008
@ Ancient Dragon, the recursive function is incorrect. foo(x) has to return int. The correct version would be

  1. int foo(int x)
  2. {
  3. if(x < 10)
  4. {
  5. x = x + 1;
  6. return foo(x);
  7. }
  8. return x;
  9. }
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Iteration vs recursion

 
0
  #4
May 15th, 2008
Iteration is when the same question keeps getting asked
Recursion is when the same question gets answered
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC