• 09-09-2008
tjpanda
A few posts down I saw a post about recursion, that looked interesting enough to try. (http://cboard.cprogramming.com/cplusplus-programming/106852-recursion.html) 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; }```
• 09-09-2008
King Mir
I don't see a way to do this without a second parameter or some other way to keep state between iterations.

In order to figure out how far to indent the 1 you need to know how many times the function has already been called. There is no way around that, and that information must be passed somehow. It can be a static variable, a global, an additional parameter, or part of a struct that is passed as the one parameter. But somehow it must be passed.
• 09-09-2008
tjpanda
Argh...well that just makes last night seem like such a waste. At least I had fun trying XD
• 09-10-2008
hk_mp5kpdw
I'd probably have a second default parameter to control the levels of indentation (a static would have to be reset somehow if you called the function more than once in the program otherwise the indentation would be messed up in successive function calls). The function could be called (from main for example) without a specified second value which would default to 0, within the function itself recursive calls would be made by specifying this parameter (and incrementing by 1 each step).

Code:

```unsigned long factorial(unsigned long n, unsigned int steps = 0) {     ...     // print out "steps" number of tab chars or something     // print out current "n"     ...     // recursive call with second parameter     return n * factorial(n-1,steps+1); } int main() {     // When calling function from within main, don't specify the second default parameter.     std::cout << factorial(4) << std::endl;     std::cout << factorial(5) << std::endl;     return 0; }```
You could use a static member, but I think you'd still need to use a default parameter to clear/reset the static value:
Code:

```unsigned long factorial(unsigned long n, bool reset = true) {     static unsigned steps;     // Either increment or reset static value based on second func arg     if( reset ) steps = 0;     else ++steps;     ...     // print out "steps" number of tab chars or something     // print out current "n"     ...     // recursive call with second function argument     return n * factorial(n-1,false); } int main() {     // When calling function from within main, don't specify the second default parameter.     std::cout << factorial(4) << std::endl;     std::cout << factorial(5) << std::endl;     return 0; }```
As to what the "local" is that's being mentioned I have no idea.