I am still learning this. Does this look right?
Thanks!!
Code:
Void DoIt(int N) {
if ( N > 0) {
cout << N << " ";
DoIt(N + 1);
}
}
Printable View
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) :D
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
}