943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 855
  • C++ RSS
Mar 14th, 2009
0

Recursion in C++

Expand Post »
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 ?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
kavithabhaskar is offline Offline
123 posts
since Jun 2008
Mar 14th, 2009
0

Re: Recursion in C++

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 14th, 2009
0

Re: Recursion in C++

Hi AncientDragon:

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

C++ Syntax (Toggle Plain Text)
  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. ~



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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
kavithabhaskar is offline Offline
123 posts
since Jun 2008
Mar 14th, 2009
0

Re: Recursion in C++

you don't want a while statement, but just a simple if statement
C++ Syntax (Toggle Plain Text)
  1. if( x > 0)
  2. countdown(x-1);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 14th, 2009
0

Re: Recursion in C++

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 ?



you don't want a while statement, but just a simple if statement
C++ Syntax (Toggle Plain Text)
  1. if( x > 0)
  2. countdown(x-1);
Reputation Points: 10
Solved Threads: 0
Junior Poster
kavithabhaskar is offline Offline
123 posts
since Jun 2008
Mar 14th, 2009
0

Re: Recursion in C++

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 14th, 2009
0

Re: Recursion in C++

I dont see the difference.. it is going to call the countdown function repetitively anyway..!
Reputation Points: 10
Solved Threads: 0
Junior Poster
kavithabhaskar is offline Offline
123 posts
since Jun 2008
Mar 14th, 2009
0

Re: Recursion in C++

With the while statement, the function will call itself billions of times. Try this version and see the results
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 14th, 2009
1

Re: Recursion in C++

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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Struct within Class, Problem with OVERLOADING OPERATORS
Next Thread in C++ Forum Timeline: did you know you can do this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC