I am still learning this. Does this look right?
Thanks!!
Code:Void DoIt(int N) { if ( N > 0) { cout << N << " "; DoIt(N + 1); } }
I am still learning this. Does this look right?
Thanks!!
Code:Void DoIt(int N) { if ( N > 0) { cout << N << " "; DoIt(N + 1); } }
*looks around*
what's it supposed to do? looks to me that if you put in any value >=1, it'll loop forever till the stack overflows, anything <1 won't even be displayed.
I am just supposed to write a recursive function, just not sure if this is "good" or not. If it was in a program would it work?
it would work, but forever(or a crash)
Last edited by prog-bman; 06-16-2004 at 11:19 AM.
Woop?
The function works fine, it recursively calls itself. However, you either a) put the limit in the wrong direction or b) forgot to put a limit in there.
If you don't have a limit in the function it will keep calling itself until your program crashes. I'd suggest putting something like if(N>=50) return; in there or something so that it stops at 50
You could also change this:to this:Code:DoIt(N + 1);Then, if you pass in any number it will print a countdown to 1.Code:DoIt(N - 1);
why not create a more useful recursive function... try writing something similar to the pow() function...
fill in the blanks...Code:long double pow(int base,int exp) { if(exp==0) return 1; else if (exp>1) //recursiveness goes here else //you figure out what goes here }
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT