20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer...

27
20-1 Computing Fundamentals Computing Fundamentals with C++ with C++ Object-Oriented Programming and Design, Object-Oriented Programming and Design, 2nd Edition 2nd Edition Rick Mercer Rick Mercer Franklin, Beedle & Associates, 1999 Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8 ISBN 1-887902-36-8 Presentation Copyright 1999, Franklin, Beedle & Associates Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Students who purchase and instructors who adopt Computing Computing Fundamentals with C++, Object-Oriented Programming and Design Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer by Rick Mercer are welcome to use this presentation as long as this copyright are welcome to use this presentation as long as this copyright notice remains intact. notice remains intact.

Transcript of 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer...

Page 1: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-1

Computing Fundamentals with C++Computing Fundamentals with C++Object-Oriented Programming and Design, 2nd EditionObject-Oriented Programming and Design, 2nd Edition

Rick MercerRick Mercer

Franklin, Beedle & Associates, 1999Franklin, Beedle & Associates, 1999

ISBN 1-887902-36-8ISBN 1-887902-36-8

Presentation Copyright 1999, Franklin, Beedle & Associates Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Students who purchase and instructors who adopt Computing Fundamentals with C++, Computing Fundamentals with C++, Object-Oriented Programming and Design Object-Oriented Programming and Design by Rick Mercer are welcome to use this by Rick Mercer are welcome to use this

presentation as long as this copyright notice remains intact.presentation as long as this copyright notice remains intact.

Page 2: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-2 Chapter 20Chapter 20RecursionRecursion

Chapter ObjectivesChapter Objectives Compare iterative and recursive solutions to the Compare iterative and recursive solutions to the

same problemsame problem Identify the recursive case and the base (or simple) Identify the recursive case and the base (or simple)

case in a recursive algorithmcase in a recursive algorithm Implement recursive methods

Page 3: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-3 20.1 Understanding Recursion20.1 Understanding Recursion

Recursion is used to describe things. Here are Recursion is used to describe things. Here are some everyday examplessome everyday examples

Show everything in a folder and all it subfoldersShow everything in a folder and all it subfolders show everything in top foldershow everything in top folder show everything in each subfolder in the same mannershow everything in each subfolder in the same manner

How to look up a word in a dictionary How to look up a word in a dictionary look up a word (use alphabetical ordering) orlook up a word (use alphabetical ordering) or look up word to define the word you are looking uplook up word to define the word you are looking up

A waiting line is either A waiting line is either empty orempty or has someone at the front of the waiting line followed by a has someone at the front of the waiting line followed by a

waiting line. waiting line. Can we have a waiting line with one person?Can we have a waiting line with one person?

Page 4: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-4Recursive definition for Recursive definition for arithmetic definition arithmetic definition

Arithmetic expression is defined asArithmetic expression is defined as a numeric constant a numeric constant an numeric identifier an numeric identifier an an arithmetic expressionarithmetic expression enclosed in parentheses enclosed in parentheses 2 2 arithmetic expressionarithmetic expressions with a binary operator like + s with a binary operator like +

- / or * - / or *

The term arithmetic expression is defined using The term arithmetic expression is defined using the term arithmetic expressionthe term arithmetic expression

but the first two possibilities don’tbut the first two possibilities don’t

Page 5: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-5 Mathematical ExamplesMathematical Examples

Consider the factorial function (0!=1)Consider the factorial function (0!=1)

The recursive definition:The recursive definition:

What is f(2)? ___________What is f(2)? ___________

What is f(3)? ___________What is f(3)? ___________

n n n n! ( ) ( ) ... 1 2 1

Page 6: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-6Recursive and non-recursive Recursive and non-recursive solutionssolutions

// Non-recursive solution, using a loop// Non-recursive solution, using a loopint factRep(int n)int factRep(int n){ { // precondition: n >= 0// precondition: n >= 0 long result = 1;long result = 1; for(int j = 2; j <= n; j++)for(int j = 2; j <= n; j++) result = j * result;result = j * result; return result;return result;} }

// Recursive solution// Recursive solutionint factRec(int n)int factRec(int n){ { // precondition: n >= 0// precondition: n >= 0 if(n == 0)if(n == 0) return 1; return 1; // Simple case// Simple case else else return n * factRec(n - 1); return n * factRec(n - 1); // Recursive case// Recursive case}}// Don't call factRec(n // Don't call factRec(n ++ 1)! 1)!

Page 7: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-7 Basic RecursionBasic Recursion

Consider this weird way to raise 2 to the 5thConsider this weird way to raise 2 to the 5th each method is a similar but simpler version of the previous methodeach method is a similar but simpler version of the previous method eventually, the method returns a value without any further callseventually, the method returns a value without any further calls

static int twoRaisedTo0() { return 1; }static int twoRaisedTo0() { return 1; } static int twoRaisedTo1() { return 2 * twoRaisedTo0(); }static int twoRaisedTo1() { return 2 * twoRaisedTo0(); } static int twoRaisedTo2() { return 2 * twoRaisedTo1(); }static int twoRaisedTo2() { return 2 * twoRaisedTo1(); } static int twoRaisedTo3() { return 2 * twoRaisedTo2(); } static int twoRaisedTo3() { return 2 * twoRaisedTo2(); } static int twoRaisedTo4() { return 2 * twoRaisedTo3(); }static int twoRaisedTo4() { return 2 * twoRaisedTo3(); } static int twoRaisedTo5() { return 2 * twoRaisedTo4(); } static int twoRaisedTo5() { return 2 * twoRaisedTo4(); }

int main() int main() {{ System.out.println("2 to the 5th = " + twoRaisedTo5());System.out.println("2 to the 5th = " + twoRaisedTo5()); }} // Output: // Output: 2 to the 5th = 322 to the 5th = 32

Page 8: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-8A Less Tedious and More General A Less Tedious and More General Option (without a loop)Option (without a loop)

Consider Consider twoRaisedTo(0)twoRaisedTo(0), which would return 1, which would return 1 Make this method return 2 with Make this method return 2 with twoRaisedTo(1)twoRaisedTo(1)

int twoRaisedTo(int n)int twoRaisedTo(int n) {{ if(n == 0)if(n == 0) return 1;return 1; elseelse // Express twoRaisedTo 4, 3, 2, 1, and 0:// Express twoRaisedTo 4, 3, 2, 1, and 0:

return 2 * __________________________________ ;return 2 * __________________________________ ; }}

How many method calls occur with How many method calls occur with twoRaisedTo(5)twoRaisedTo(5)??

Page 9: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-9 Recursive methodsRecursive methods

The previous method is known as a recursive The previous method is known as a recursive method because it calls itselfmethod because it calls itself

Each time, the method calls a simpler versionEach time, the method calls a simpler version The answers should beThe answers should be

return 2 *return 2 * twoRaisedTo(n-1); twoRaisedTo(n-1); and and 55

Eventually no further calls are necessaryEventually no further calls are necessary the simple case is reached and the method simplythe simple case is reached and the method simply

returns returns 11 which goes back to the previous callwhich goes back to the previous call

return 2 * return 2 * twoRaisedTo(1)twoRaisedTo(1); // 2 * ; // 2 * 11 = 2 = 2

Page 10: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-10 Mathematical ExamplesMathematical Examples

twoRaisedTo can be written as followstwoRaisedTo can be written as follows

If n >= 0, use the top line as the definition of the If n >= 0, use the top line as the definition of the methodmethod

If n == 0, use the bottom lineIf n == 0, use the bottom line Notice that twoRaisedTo appears on both sides of =, Notice that twoRaisedTo appears on both sides of =,

which makes this a which makes this a recursive definitionrecursive definition Recursive definitions must have a Recursive definitions must have a simple casesimple case

no method name to the right of = no method name to the right of =

Page 11: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-11 Complete the SubstitutionsComplete the Substitutions

Can you evaluate f(5) before evaluating f(4)? Can you evaluate f(5) before evaluating f(4)? Now perform back substitutionNow perform back substitution

fill in the blanks above fill in the blanks above from top to bottomfrom top to bottom

twoRaisedTo(0) = ____ // simple case: The first answer you can write

twoRaisedTo(1) = 2 * twoRaisedTo(0) = 2 * ____

twoRaisedTo(2) = 2 * twoRaisedTo(1) = 2 * ____

twoRaisedTo(3) = 2 * twoRaisedTo(2) = 2 * ____

twoRaisedTo(4) = 2 * twoRaisedTo(3) = 2 * ____

twoRaisedTo(5) = 2 * twoRaisedTo(4) = 2 * _____ or

Page 12: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-1220.2 Defining Base and 20.2 Defining Base and Recursive CasesRecursive Cases

When writing recursive methodsWhen writing recursive methods Make sure there is at least one base caseMake sure there is at least one base case

at least one situation where a recursive call is at least one situation where a recursive call is not not made. There could be more than 1made. There could be more than 1

– The method might return a value, or do nothing at allThe method might return a value, or do nothing at all

There could be one or more recursive casesThere could be one or more recursive cases a recursive call must be a simpler version of the a recursive call must be a simpler version of the

same problemsame problem– the recursive call should bring the method closer to the recursive call should bring the method closer to

the base case the base case perhaps pass perhaps pass n-1n-1 rather than rather than nn..

Page 13: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-13 How Recursion worksHow Recursion works

Method calls generate activation records Method calls generate activation records Depending on the system, the activation record Depending on the system, the activation record

might storemight store all parameter values and local valuesall parameter values and local values return point -- where to go after the method finishesreturn point -- where to go after the method finishes

imagine all this is in a boximagine all this is in a box the data is stored in an activation frame and pushed the data is stored in an activation frame and pushed

onto a stack -- one on top of the otheronto a stack -- one on top of the other when the method finishes, the activation frame stack when the method finishes, the activation frame stack

is popped is popped it disappears, control returns to where it was calledit disappears, control returns to where it was called

Page 14: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-14 A method that calls itselfA method that calls itself

void forward(int n)void forward(int n){{ if(n > 1)if(n > 1) forward(n - 1); forward(n - 1); // recursive call: n goes toward 0// recursive call: n goes toward 0 // RP# FORWARD // RP# FORWARD System.out.println(n);System.out.println(n);}}

int main()int main(){ { int arg = 3;int arg = 3; forward(arg);forward(arg); // RP# MAIN// RP# MAIN arg = 999;arg = 999; return 0;return 0;}}

RP# MAINRP# MAINarg 3arg 3

RP# MAINRP# MAINarg 3arg 3

RP# MAINRP# MAINarg 3arg 3

RP# FORWARDRP# FORWARDn 3n 3

RP# MAINRP# MAINarg 3arg 3

RP# FORWARDRP# FORWARDn 3n 3

RP# FORWARDRP# FORWARDn 3n 3

RP# FORWARDRP# FORWARDn 2n 2

RP# FORWARDRP# FORWARDn 2n 2

n 1n 1

Page 15: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-15 The simple case is reachedThe simple case is reached

Several activation frames are stackedSeveral activation frames are stackedWhen parameter (n) == 1, there is no When parameter (n) == 1, there is no

recursive call.recursive call. (1) execute the base case when n == 1(1) execute the base case when n == 1

System.out.println(n);System.out.println(n);

The output is The output is 11 and the method is done and the method is done

RP# MAINRP# MAINarg 3arg 3

RP# FORWARDRP# FORWARDn 3n 3

RP# FORWARDRP# FORWARDn 2n 2

n 1n 1

(1)

Page 16: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-16 Returning back to mainReturning back to main (2) Return to previous call and pop box on top(2) Return to previous call and pop box on top

continue from RP# FORWARD, print 2 continue from RP# FORWARD, print 2 (3) Return to previous call and pop box on top(3) Return to previous call and pop box on top

continue from RP# FORWARD, print 3 continue from RP# FORWARD, print 3 (4) Return to main, pop box on top(4) Return to main, pop box on top

execute arg = 999 (to indicate we're back in main)execute arg = 999 (to indicate we're back in main)

The main method is done, Output is The main method is done, Output is 123123

RP# MAINRP# MAINarg 3arg 3

RP# FORWARDRP# FORWARDn 3n 3

RP# FORWARDRP# FORWARDn 2n 2

RP# MAINRP# MAINarg 3arg 3

RP# FORWARDRP# FORWARDn 3n 3

RP# MAINRP# MAINarg 3 999arg 3 999

(2)

(3)

(4)

Page 17: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-17Too much recursion can be Too much recursion can be costlycostly

Consider the Fibonacci numbersConsider the Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, ….1, 1, 2, 3, 5, 8, 13, 21, ….

Demonstrate Demonstrate Fibonacc.cppFibonacc.cpp

long fib(int n)long fib(int n){ { // counts calls to fib// counts calls to fib methodCalls++; methodCalls++; if(n == 0 || n == 1)if(n == 0 || n == 1) return 1;return 1; else else return fib(n-1)+fib(n-2);return fib(n-1)+fib(n-2);} }

Page 18: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-18Fib(5) calls fib(4) once, fib(3) Fib(5) calls fib(4) once, fib(3) twice, fib(2) thrice, fib (1) five twice, fib(2) thrice, fib (1) five times and fib(0) thrice times and fib(0) thrice

fib(7) = 21 methodCalls = 41fib(7) = 21 methodCalls = 41fib(8) = 34 methodCalls = 67fib(8) = 34 methodCalls = 67fib(9) = 55 methodCalls = 109fib(9) = 55 methodCalls = 109fib(10) = 89 methodCalls = 177fib(10) = 89 methodCalls = 177fib(11) = 144 methodCalls = 287fib(11) = 144 methodCalls = 287fib(12) = 233 methodCalls = 465fib(12) = 233 methodCalls = 465fib(13) = 377 methodCalls = 753fib(13) = 377 methodCalls = 753fib(14) = 610 methodCalls = 1219fib(14) = 610 methodCalls = 1219fib(15) = 987 methodCalls = 1973fib(15) = 987 methodCalls = 1973fib(16) = 1597 methodCalls = 3193fib(16) = 1597 methodCalls = 3193fib(17) = 2584 methodCalls = 5167fib(17) = 2584 methodCalls = 5167fib(18) = 4181 methodCalls = 8361fib(18) = 4181 methodCalls = 8361fib(19) = 6765 methodCalls = 13529fib(19) = 6765 methodCalls = 13529fib(20) = 10946 methodCalls = 21891fib(20) = 10946 methodCalls = 21891

Page 19: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-19 Recursive Fibonacci grows Recursive Fibonacci grows ExponentiallyExponentially

Recursive Fibonacci

0

5000000

10000000

15000000

20000000

25000000

30000000

Fibonacci#

Met

hod

Cal

ls

Fib#Method Calls

Page 20: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-20 20.3 Converting Decimal 20.3 Converting Decimal Numbers to Other BasesNumbers to Other Bases

Problem: Convert a decimal (base 10) number Problem: Convert a decimal (base 10) number into other basesinto other bases

Function Call Output Function Call Output convert(99, 2) 1100011 convert(99, 2) 1100011 convert(99, 3) 10200 convert(99, 3) 10200 convert(99, 4) 1203 convert(99, 4) 1203 convert(99, 5) 344 convert(99, 5) 344 convert(99, 6) 243 convert(99, 6) 243 convert(99, 7) 201 convert(99, 7) 201 convert(99, 8) 143 convert(99, 8) 143 convert(99, 9) 120 convert(99, 9) 120

Page 21: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-21 Digits are multiplied by powers Digits are multiplied by powers of the base of the base 10, 8, 2, or whatever10, 8, 2, or whatever

First: converting from other bases to decimalFirst: converting from other bases to decimal Decimal numbers multiply digits by powers of 10 Decimal numbers multiply digits by powers of 10

950795071010 = 9 x 10 = 9 x 1033 + 5 x 10 + 5 x 1022 + 0 x 10 + 0 x 1011 + 7 x 10 + 7 x 1000

Octal numbers Octal numbers powers of 8 powers of 8

1567156788 = 1 x 8 = 1 x 833 + 5 x 8 + 5 x 822 + 6 x 8 + 6 x 811 + 7 x 8 + 7 x 800

= 512 = 512 + 320 + 320 + 48 + 48 + 7 = 887+ 7 = 8871010

Binary numbers Binary numbers powers of 2 powers of 2

1011101122 = 1 x 2 = 1 x 233 + 1 x 2 + 1 x 222 + 1 x 2 + 1 x 211 + 1 x 2 + 1 x 200 = 8 + 4 = 8 + 4 + 2 + 2 + 1 = 15+ 1 = 151010

Page 22: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-22 Converting base 10 to base 2Converting base 10 to base 2

1) divide number by new base (2), write remainder (1)1) divide number by new base (2), write remainder (1)

2) divide quotient (2), write new remainder (0) to left2) divide quotient (2), write new remainder (0) to left

3) divide quotient (1), write new remainder (1) to left3) divide quotient (1), write new remainder (1) to left

__2___2_ 2) 5 Remainder = 1 2) 5 Remainder = 1 __1_ __1_ 2) 2 Remainder = 0 2) 2 Remainder = 0 __0_ __0_ 2) 1 Remainder = 12) 1 Remainder = 1

Stop when the quotient is 0 Stop when the quotient is 0 5 51010 = = 10110122

Print remainders in reverse order

Page 23: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-23 Converting base 10 to base 8Converting base 10 to base 8

1) divide number by new base (8), write remainder (1)1) divide number by new base (8), write remainder (1)

2) divide quotient (2), write new remainder (0) to left2) divide quotient (2), write new remainder (0) to left

3) divide quotient (1), write new remainder (1) to left3) divide quotient (1), write new remainder (1) to left

_12__12_ 8)99 Remainder = 3 8)99 Remainder = 3 __1_ __1_ 8)12 Remainder = 4 8)12 Remainder = 4 __0_ __0_ 8) 1 Remainder = 18) 1 Remainder = 1

Stop when the quotient is 0 Stop when the quotient is 0 99 991010 = = 13413488

Print remainders in reverse order

Page 24: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-24 SolutionsSolutions

We could eitherWe could either store remainders in an array and reverse it orstore remainders in an array and reverse it or write out the remainders in reverse order write out the remainders in reverse order

have to postpone the output until we get quotient = 0have to postpone the output until we get quotient = 0

AlgorithmAlgorithm while the decimal number > 0 while the decimal number > 0 {{ Divide the decimal number by the new base Divide the decimal number by the new base Set the decimal number to the decimal number divided by the base Set the decimal number to the decimal number divided by the base Write the remainder to the left of any preceding remainders Write the remainder to the left of any preceding remainders }}

Page 25: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-25Base CaseBase CaseRecursive CaseRecursive Case

Base caseBase case if decimal number being converted = 0if decimal number being converted = 0

do nothingdo nothing

Recursive caseRecursive case if decimal number being converted > 0if decimal number being converted > 0

convert a simpler version of the problemconvert a simpler version of the problem– use the quotient as the argument to the next calluse the quotient as the argument to the next call

print the remainderprint the remainder

Page 26: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-26Convert a number to other Convert a number to other basesbases

void convert(int decimalNumber, int base)void convert(int decimalNumber, int base){{ if(decimalNumber > 0)if(decimalNumber > 0) { { // This recursive call makes progress // This recursive call makes progress // towards the simple case by passing// towards the simple case by passing // the quotient in each recursive call// the quotient in each recursive call convert(decimalNumber / base, base);convert(decimalNumber / base, base);// RP# CONVERT// RP# CONVERT System.out.print(decimalNumber % base);System.out.print(decimalNumber % base); // After the last call, write the // After the last call, write the // remainders in reverse order// remainders in reverse order }}}}

Demonstrate Fibonacci.cpp

Page 27: 20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

20-27 99 base 899 base 8

// Base case where // Base case where // nothing happens, so return// nothing happens, so return

// print 1// print 1number % 8 = 1 % 8 = number % 8 = 1 % 8 = 11

// print 4// print 4number % 8 = 12 % 8 =number % 8 = 12 % 8 = 4 4

// print 3// print 3number % 8 = 99 % 8 = number % 8 = 99 % 8 = 33

// Output from inside the if // Output from inside the if 143143

IN MAININ MAINnumber 99number 99base 8base 8

RP# CONVERTRP# CONVERTnumber 99number 99base 8base 8

RP# CONVERTRP# CONVERTnumber 12number 12base 8base 8

RP# CONVERTRP# CONVERTnumber 1number 1base 8base 8

number 0number 0base 8base 8