A recursive routine for printing N in decimal form

Creating ANSI/AIM Code 39 In JavaUsing Barcode creation for Java Control to generate, create Code 3/9 image in Java applications.

1 / / Print n in any base 2 / / Assumes 2 <= base <= 16 3 void printInt( int n , int base

Bar Code Maker In JavaUsing Barcode generator for Java Control to generate, create bar code image in Java applications.

static string DIGIT-TABLE

Read Bar Code In JavaUsing Barcode reader for Java Control to read, scan read, scan image in Java applications.

"0123456789abcdef";

Code 3 Of 9 Generation In C#Using Barcode drawer for Visual Studio .NET Control to generate, create Code 3/9 image in VS .NET applications.

6 7 8

Generate ANSI/AIM Code 39 In VS .NETUsing Barcode encoder for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.

9 10

Encode Code-39 In .NET FrameworkUsing Barcode generation for .NET Control to generate, create Code 39 Extended image in .NET framework applications.

if ( n > = base ) printInt( n / base, base ) ; cout << DIGIT-TABLE[ n % base 1 ;

Draw Code 3 Of 9 In Visual Basic .NETUsing Barcode printer for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET applications.

A recursive routine for printing N in any base

Barcode Encoder In JavaUsing Barcode creation for Java Control to generate, create barcode image in Java applications.

Basic ~ e c u r K m

Draw Code128 In JavaUsing Barcode generation for Java Control to generate, create Code 128 image in Java applications.

The most interesting error occurs when base is I Then the recursive call at line 8 fails to make progress because the two parameters to the recursive call are identical to the original call Thus the system makes recursive calls until it eventually runs out of bookkeeping space (and exits less than gracefully) We can make the routine more robust by adding an explicit test for baseThe problem with this strategy is that the test would be executed during each of the recursive calls to printInt,not just during the first call Once base is valid in the first call, to retest it is silly because it does not change in the course of the recursion and thus must still be valid One way to avoid this inefficiency is to set up a driver routine A driver routine tests the validity of base and then calls the recursive routine, as shown in Figure 84 The use of driver routines for recursive programs is a common technique

Encode EAN-13 Supplement 5 In JavaUsing Barcode creator for Java Control to generate, create European Article Number 13 image in Java applications.

Failure to progress means that the program does not work

Draw Bar Code In JavaUsing Barcode generator for Java Control to generate, create bar code image in Java applications.

A driver routine tests the validity of the first call and the calls the recursive routine

Paint Barcode In JavaUsing Barcode maker for Java Control to generate, create barcode image in Java applications.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Make International Standard Serial Number In JavaUsing Barcode drawer for Java Control to generate, create ISSN - 10 image in Java applications.

const string DIGIT-TABLE const int MAX-BASE

Make Code 128A In VS .NETUsing Barcode generator for ASP.NET Control to generate, create Code 128A image in ASP.NET applications.

"0123456789abcdefU; DIGIT-TABLElength( )

Data Matrix ECC200 Scanner In VS .NETUsing Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.

/ / Print n in base base, recursively / / Precondition: n >= 0 , 2 <= base < = MAX-BASE

Code-39 Creation In C#Using Barcode generation for Visual Studio .NET Control to generate, create USS Code 39 image in VS .NET applications.

void printIntRec( int n , int base

Paint Bar Code In VS .NETUsing Barcode maker for .NET Control to generate, create bar code image in .NET applications.

if( n > = base ) printIntRec( n / base, base cout < < DIGIT-TABLE[ n % base 1 ;

Decoding Code 39 In Visual Studio .NETUsing Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.

/ / Driver routine void printInt ( int n , int base

Encode Bar Code In Visual Basic .NETUsing Barcode creation for .NET Control to generate, create bar code image in .NET applications.

if( base <= 1 I I base > MAX-BASE ) cerr << "Cannot print in base " << base << endl; else

UPC-A Supplement 2 Printer In Visual Studio .NETUsing Barcode maker for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications.

if( n i 0

{ ) "-";

cout << n = -n;

printIntRec( n , base

A robust number printing program

832 Why It Works

Recursivealgorithms can be proven correct with mathematical induction

In Theorem 83 we show, somewhat rigorously, that the printDecima1 lorks Our goal is to verify that the algorithm is correct, so the proof is bast:d on the assumption that we have made no syntax errors

Theorem 83

The algorithm prin tDecimal shown in Figure 82 correctly prints n in base 10

Proof

Let k he the number of digits in n The proof is by induction on k Basis: If k = 1, then no recursive call is made, and line 6 correctly outputs the one digit o f n Inductive Hypothesis: Assume that printDecima1 works correctly for all k 2 1 digit integers We show that this assumption implies correctness for any k + 1 digit integer nBecause k 2 1, the if statement at line 4 is satzsfied for a k + I digit integer nBy the inductive hypothesis, the recur~ive at line 5 prints the$rst k digits of n Then the call at line 6 call prints the final digit Thus ifany k digit integer can be printed, then so can a k + 1 digit integer: By induction, we conclude that prlntDecima1 works for all k, and thus all n

The proof of Theorem 83 illustrates an important principle When designing a recursive algorithm, we can always assume that the recursive calls work (if they progress toward the base case) because, when a proof is performed, this assumption is used as the inductive hypothesis At first glance such an assumption seems strange However, recall that we always assume that function calls work, and thus the assumption that the recursive call works is really no different Like any function, a recursive routine needs to combine solutions from calls to other functions to obtain a solution However, other functions may include easier instances of the original function This observation leads us to the third fundamental rule of recursion

3 "You gotta believe ":Always assume that the recursive call works

Basic Recursion

Rule 3 tells us that when we design a recursive function, we do not have to attempt to trace the possibly long path of recursive calls As we showed earlier, this task can be daunting and tends to make the design and verification more difficult A good use of recursion makes such a trace almost impossible to understand Intuitively, we are letting the computer handle the bookkeeping that, were we to do ourselves, would result in much longer code This principle is so important, that we state it again: Always assume that the recursive call works

The third fundamental rule of recursion: Always assume that the recursive call works Use this rule to design your algorithms