A few posts down I saw a post about recursion, that looked interesting enough to try. (Recursion) The problem presented was:
It should print its local variable and recursive call parameter. For each recursive call, display the outputs on a separate line and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful.
I wasn't sure if I should post code in that thread or not because it was a homework thread and I'm not sure what the rules are in relation to that so I figured I'd be safe rather than sorry and open a new thread. Having stated that I think I am rather confused by where a local variable comes into play here. I think it's going to play a key-role in determining the tab stop...but without adding a second parameter I'm not entirely sure how. (I don't know if it allows you to add a second parameter or not, but that would make it too easy and the problem would lose it's challenge!)
The code I have right now doesn't show any attempts at coming up with a tab system in place, because I keep deleting every attempt I've made so far. Just to clarify really quickly: this isn't my homework, it IS a homework problem but I am taking no programming classes. I just want to do it because it looks like a fun challenge, but I'm stuck and that's why I'm here. Any and all code snippets/or ideas/ways to solve/hints/etc are welcome!
Code:// Our recursive function unsigned long Factorial( unsigned long n ) { // check to see if we need to stop if ( n <= 0 ) return 1; else { // we're good so keep going std::cout << n << std::endl; return n * Factorial( n-1 ); } } int _tmain(int argc, _TCHAR* argv[]) { std::cout << Factorial(4); // result should be something like: /* 4 3 2 1 4*3*2*1 = 24 */ // don't close the box automatically int x; std::cin >> x; return 0; }



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.