| | |
Recursion in C++
![]() |
•
•
Join Date: Jun 2008
Posts: 117
Reputation:
Solved Threads: 0
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)
#include<iostream.h> void countdown(int x) { using namespace std; cout<<x<<endl; countdown(x-1); } int main(void) { countdown(10); return 0; }
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
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.
•
•
Join Date: Jun 2008
Posts: 117
Reputation:
Solved Threads: 0
Hi AncientDragon:
Thanks for ur reply.. i made some modifications.. still does not work! i get all 0s now..
Thanks for ur reply.. i made some modifications.. still does not work! i get all 0s now..
C++ Syntax (Toggle Plain Text)
#include<iostream.h> using namespace std; void countdown(int x) { cout<<x<<endl; while(x > 0) { countdown(x-1); } } int main(void) { countdown(10); return 0; } ~ ~ ~
•
•
•
•
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 lineusing namespace std;up above just under the last include directive. It does not good to place it where it is.
you don't want a while statement, but just a simple if statement
C++ Syntax (Toggle Plain Text)
if( x > 0) 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.
•
•
Join Date: Jun 2008
Posts: 117
Reputation:
Solved Threads: 0
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)
if( x > 0) countdown(x-1);
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.
With the while statement, the function will call itself billions of times. Try this version and see the results
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; long long counter = 0; void countdown(int x) { ++counter; if( counter % 10000000 == 0) cout << counter << "\n"; //cout<<x<<endl; while(x > 0) { countdown(x-1); } } int main(void) { countdown(10); cout << "counter = " << counter << "\n"; return 0; }
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.
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
and open form as
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.
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
and open form as
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
(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
![]() |
Similar Threads
- Recursion: when do you use it? (C++)
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- Recursion (C)
Other Threads in the C++ Forum
- Previous Thread: Change text color using visual c++
- Next Thread: did you know you can do this?
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






