### Lab 11: practice recursion

• Introduction

• This lab will reinforce recursion which is traditionally a pretty complicated technique for students to master in Computer Science.

• Preparation

• Copy-and-paste these commands to your terminal. They copy the files and cd to the lab directory:

 ``` mkdir ~/cs170/lab11 cd ~cs170002/share/lab11 cp *.java ~/cs170/lab11 cd ~/cs170/lab11 ```

We will learn to write 2 recursive algorithms in this lab.

But first, some theory on how to think about recursive algorithms.

• How to write recursive methods

• As discussed in class, a recursive method will invoke itself, but the invocation always uses a parameter value that is smaller that the original value.

Example: factorial

 ``` public static int factorial( int n ) { int sol1, mySol; if ( n == 0 ) { return 1; // A solution is available for a base case } else { sol1 = factorial ( n - 1 ); mySol = n * sol1; return mySol; } } ```

The original parameter is n, and factorial(n) invokes itself with a smaller parameter value (n−1)

• The key in understanding recusive methods is recognizing that you are:

 Not:   "solving a smaller problem" to solve the original problem But:   "using the solution of a smaller problem" to solve the original problem.

So:

 Think of the invocation factorial(n-1) as something that has been completed. And it has return the solution of the smaller problem that you can use.

• Example: compute 20!

• We know that: 20! = 20 × 19!

• Suppose we know that: 19! = 121645100408832000, then we can use the solution of this smaller problem (19!) to solve the original problem 20!:

 ``` 20! = 20 × 121645100408832000 = 2432902008176640000 ```

• The Base case(s)

• In every recursive method, there is at least one or more base cases that must have been solved (i.e., you know the solution)

 The readily solved problems are called base cases.

• General rule:

 If you use the solution of the smaller problem Problem(n − k) to solve the problem Problem(n), you will need k base cases.

• Structure of a recursive method

• The easiest recursive method uses one smaller problem to solve the original problem.

• The structure of a recursive method to solve a problem of size n using the solution of a problem of size n−1 is:

 ``` public static ReturnType solveProblem( int n ) { ReturnType sol1, mySol; if ( n is a base case ) { return solution; // A solution is available for a base case } else { sol1 = solveProblem ( n - 1 ); mySol = solve problem of size n using solution sol1; return mySol; } } ```

Example: factorial

 ``` public static int factorial( int n ) { int sol1, mySol; if ( n == 0 ) { return 1; // A solution is available for a base case } else { sol1 = factorial ( n - 1 ); mySol = n * sol1; return mySol; } } ```

• Problem description:

• Write a recursive method power(int a, int n) that computes an

• Steps:

• Edit the file Power.java and write the method power(int a, int n ). (See hints on how to do it below)

 ``` gedit Power.java & (or use BlueJ) ```

• Compile with the command:

 ``` javac Power.java (or click compile in BlueJ) ```

• Run with the command:

 ``` java Power (or select the main() method to run in BlueJ) ```

• How to approach the problem:

1. What is the problem solved by the method ?

 ``` power( a, n ) computes an (example: power( 2, 10 ) computes 210) ```

2. What is a smaller problem of the same nature ?

 ``` power( a, n-1 ) computes an-1 (example: power( 2, 9 ) computes 29) ```

3. How can you use the solution of power( a, n−1 ) to solve the original problem ?

 ``` If we are solving 210 and we know the solution 29 = 512, we can solve 210 as follows: 2×512 In general: If we are solving an and we know the solution an-1 = sol1, we can solve an as follows: a×sol1 ```

4. What is a base case that we have a ready solution ?

 ``` power( a, 0 ) is equal to 1 (example: power( 2, 0 ) is equal to 1) ```

Use the above information to write the recursive method power(int a, int n)

 Do not use any loops in the method !!!

• Other kinds of smaller problems

• The most common smaller problem that a recursive method solves within itself uses the parameter n−1

But, that is not always the case.

• Problems involving strings that are solved by recursive method usually uses a shorter length string.

The most common smaller problem that a "string typed" recursive method solves within itself uses a string of shorter length n−1.

• A recursion exercise using "string" typed input

• Problem description:

• Given a non-negative number n.

Write a recursive method sumDigits(int n) that computes sum of the digits in the number n.

• Steps:

• Edit the file SumDigits.java and write the method sumDigits(int n ). (See hints on how to do it below)

 ``` gedit SumDigits.java & (or use BlueJ) ```

• Compile with the command:

 ``` javac SumDigits.java (or click compile in BlueJ) ```

• Run with the command:

 ``` java SumDigits (or select the main() method to run in BlueJ) ```

• How to approach the problem:

1. It is easier to describe the method by listing out the digits of a number.

So I will use the notation:

 ``` n = xyz ```

to represent the fact that the number n consists of a number of digits xyz (it does not mean 3 digits, what I mean is: "some digits")

2. What is the problem solved by the method ?

 ``` sumDigits( n = xyz ) computes x+y+z (example: sumDigits( 3412 ) computes 3+4+1+2) ```

3. What is a smaller problem of the same nature ?

 ``` sumDigits( n/10 = xy ) computes x+y (example: sumDigits( 341 ) computes 3+4+1) (Note that: 341 = 3412/10 So the operation to shorten a number by 1 digit is "dividing by 10" !) ```

4. How can you use the solution of sumDigits(n/10) to solve the original problem ?

 ``` If we are solving sumDigit( 3412 ) and we know the solution sumDigit( 341 ) = 8, we can solve sumDigit( 3412 ) as follows: 8 + 2 (2 = the last digit of 3412) or: 8 + (3412 % 10) In general: If we are solving sumDigit( n = xyz ) and we know the solution sumDigit( n/10 = xy ) = sol1, we can solve sumDigit( n = xyz ) as follows: sol1 + z (z = the last digit of n = xyz) or: sol1 + (n % 10) ```

5. What is a base case that we have a ready solution ?

 ``` sumDigits( 0 ) is equal to 0 ```

Use the above information to write the recursive method sumDigits(int n)

 Do not use any loops in the method !!!

• Turnin

• Use the following command to turn in your lab 11:

 ``` cd ~/cs170/lab11 /home/cs170002/turnin-lab Power.java lab11 /home/cs170002/turnin-lab SumDigits.java lab11a ```