Recursion in C++

Reply

Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Recursion in C++

 
0
  #1
Mar 14th, 2009
HI I am trying to work on a very simple recursive function but i get o/p as some negative values.. can u please help and tell me what mistake i am doing ?

  1. #include<iostream.h>
  2.  
  3.  
  4. void countdown(int x)
  5. {
  6. using namespace std;
  7. cout<<x<<endl;
  8. countdown(x-1);
  9. }
  10.  
  11. int main(void)
  12. {
  13. countdown(10);
  14. return 0;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Recursion in C++

 
0
  #2
Mar 14th, 2009
1) it goes negative because there is nothing to prevent it. Its an infinit recursive function -- that is until it consumes all available stack space and then it will simply crash. You need to add a line that stops the recursion when x == 0 (or whatever other value you want).

2) replace iostream.h with <iostream>. Current c++ standards do not use the .h extension. If you compiler doesn't support <iostream> then get a newer compiler.

3) move that line using namespace std; up above just under the last include directive. It does not good to place it where it is.
Last edited by Ancient Dragon; Mar 14th, 2009 at 12:19 am.
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: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Recursion in C++

 
0
  #3
Mar 14th, 2009
Hi AncientDragon:

Thanks for ur reply.. i made some modifications.. still does not work! i get all 0s now..

  1. #include<iostream.h>
  2.  
  3. using namespace std;
  4. void countdown(int x)
  5.  
  6. {
  7. cout<<x<<endl;
  8. while(x > 0)
  9. {
  10. countdown(x-1);
  11. }
  12. }
  13.  
  14. int main(void)
  15. {
  16. countdown(10);
  17. return 0;
  18. }
  19. ~
  20. ~
  21. ~



Originally Posted by Ancient Dragon View Post
1) it goes negative because there is nothing to prevent it. Its an infinit recursive function -- that is until it consumes all available stack space and then it will simply crash. You need to add a line that stops the recursion when x == 0 (or whatever other value you want).

2) replace iostream.h with <iostream>. Current c++ standards do not use the .h extension. If you compiler doesn't support <iostream> then get a newer compiler.

3) move that line using namespace std; up above just under the last include directive. It does not good to place it where it is.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Recursion in C++

 
0
  #4
Mar 14th, 2009
you don't want a while statement, but just a simple if statement
  1. if( x > 0)
  2. countdown(x-1);
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: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Recursion in C++

 
0
  #5
Mar 14th, 2009
Hey THanks it worked.. but how does it matter.. "while" was supposed to be doing the same thing..as "if" is doing now.. then why did it falter ?



Originally Posted by Ancient Dragon View Post
you don't want a while statement, but just a simple if statement
  1. if( x > 0)
  2. countdown(x-1);
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Recursion in C++

 
0
  #6
Mar 14th, 2009
The while didn't work because now you have recursion within recursion. Follow the function through with pencil & paper and you will see why it doesn't work like you wanted it to.
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: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Recursion in C++

 
0
  #7
Mar 14th, 2009
I dont see the difference.. it is going to call the countdown function repetitively anyway..!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Recursion in C++

 
0
  #8
Mar 14th, 2009
With the while statement, the function will call itself billions of times. Try this version and see the results
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. long long counter = 0;
  6. void countdown(int x)
  7. {
  8. ++counter;
  9. if( counter % 10000000 == 0)
  10. cout << counter << "\n";
  11. //cout<<x<<endl;
  12. while(x > 0)
  13. {
  14. countdown(x-1);
  15. }
  16. }
  17.  
  18. int main(void)
  19. {
  20. countdown(10);
  21. cout << "counter = " << counter << "\n";
  22. return 0;
  23. }
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: Oct 2007
Posts: 791
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 134
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Recursion in C++

 
1
  #9
Mar 14th, 2009
Oh man!! Ancient Dragon would be snatching his hairs!!

Look kavithabaskar, You are basically using recursions to avoid loops.
Every series has Recursive form and a closed form(non-recursive)[Note that it is not possible for every series to have both forms].
Say, the series 1,2,3,4,5,6,7,8 has a recursive form as A_{n}=A_{n-1}+1 || A_{1}=1
and open form as A_{n}=n
So, if you know the closed form it is best to use it through loops.
But when you dont know closed form you go to recursive functions.
Last edited by siddhant3s; Mar 14th, 2009 at 2:47 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
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