(Visualizing Recursion) It’s interesting to watch recursion “in action.” Modify the factorial function of Fig. 5.28 to 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. Your goal here is to design and implement an output for- mat that helps a person understand recursion better. You may want to add such display capabilities to the many other recursion examples and exercises throughout the text.

What will be an ideal response?


```
// Visualizing recursion with factorial function.
#include
#include
using namespace std;

unsigned long factorial( unsigned long ); // function prototype

int main()
{
// Loop 10 times. During each iteration, calculate
// factorial( i ) and display result.
for ( int i = 0; i <= 10; i++ )
{
cout << "Calculating factorial ( " << i << " )" << endl;
unsigned long result = factorial( i );
cout << setw( 2 ) << i << "! = " << result << endl << endl;
} // end for
} // end main

// recursive definition of function factorial
unsigned long factorial( unsigned long number )
{
if ( number <= 1 ) // base case
{
cout << " Reached base case of 1" << endl;
return 1;
} // end if
else // recursion step
{
// add outputs and indentation to help visualize recursion
cout << setw( number * 3 ) << ""
<< "local variable number: " << number << endl;
cout << setw( number * 3 ) << ""
<< "recursively calling factorial( "
<< number - 1 << " )" << endl << endl;
return ( number * factorial( number - 1 ) );
} // end else
} // end function factorial
```

Computer Science & Information Technology

You might also like to view...

A is a special variable that receives a value being passed into a procedure or function.

a. temporary variable b. pseudo-constant c. class-level variable d. parameter

Computer Science & Information Technology

If you need to modify the formula in a calculated column, you edit the formula in one cell of the column and then you modify the formulas in the remaining cells in that table column.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

The feature to choose how much of a document, presentation, or worksheet you view on the screen

What will be an ideal response?

Computer Science & Information Technology

Kathleen is implementing an access control system for her organization and builds the following array: Reviewers: update files, delete files Submitters: upload files Editors: upload files, update files Archivists: delete files What type of access control system has Kathleen implemented?

A. Role-based access control B. Task-based access control C. Rule-based access control D. Discretionary access control

Computer Science & Information Technology