

But:

Explanation:


Characteristics:

Characteristics:


int fac( int n ) // fac(n) is **me** { int mySol; int Sol1; if ( n == 0 ) { // It's simple enough that I can solve it return 1; } else { Sol1 = somefacsolver( n  1 ); // Delegate smaller problem away // and receive his solution mySol = n * Sol1; // Use the helper's solution to // solve my problem return mySol; } } 
Wait a minite.... I can use fac( ) as somefacsolver( ) !!!
int fac( int n ) // fac(n) is **me** { int mySol; int Sol1; if ( n == 0 ) { // It's simple enough that I can solve it return 1; } else { Sol1 = fac( n  1 ); // Delegate smaller problem away // and receive his solution mySol = n * Sol1; // Use the helper's solution to // solve my problem return mySol; } } 
Then I execute a small example (e.g., fac(2)) and show student the "recursion"

String hanoi( int ndisks, int from, int to, int help ) { String MySol, Sol1, Sol2; if ( ndisks == 1 ) { return from + " > " + to + "\n"; // Small enough // Solve it myself } else { Sol1 = somehanoisolver( ndisks1, from, help, to ); // Find sol1 Sol2 = somehanoisolver( ndisks1, help, to, from ); // Find sol2 MySol = Sol1 + (from + " > " + to + "\n") + Sol2; return MySol; } } 
Wait a minite.... I can use hanoi( ) as somehanoisolver( ) !!!
String hanoi( int ndisks, int from, int to, int help ) { String MySol, Sol1, Sol2; if ( ndisks == 1 ) { return from + " > " + to + "\n"; // Small enough // Solve it myself } else { Sol1 = hanoi( ndisks1, from, help, to ); // Find sol1 Sol2 = hanoi( ndisks1, help, to, from ); // Find sol2 MySol = Sol1 + (from + " > " + to + "\n") + Sol2; return MySol; } } 
Running hanoi( ) is not a good idea.
Rather, to show them the steps of the algorithm, I use an applet: click here
(Make sure to stop half way and show students the smaller solutions)