The pool rack example could be implemented using a for loop. It is also possible to write recursive...

15
10.4 Recursion and Looping, Recursion with Two Parameters

Transcript of The pool rack example could be implemented using a for loop. It is also possible to write recursive...

Page 1: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

10.4 Recursion and Looping, Recursion with Two Parameters

Page 2: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

The pool rack example could be implemented using a for loop.

It is also possible to write recursive methods that accomplish things that you might do with a while loop.

Recursion can be Loops

Page 3: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

A recursive definition is given below for finding how many times the constant value 2 will go evenly into another number.

The value 1 which is added in the recursive case counts how many times recursion occurs.

This count gives the result of the division. Notice that in the recursive case n is no

longer decremented by 1. It is decremented by 2.

Recursion with a Different Base Case

Page 4: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

The test for the base case is an inequality rather than an equality since the values that n takes on vary depending on whether it is initially odd or even.

Recursive case: n >= 2 f(n) = 1 + f(n – 2) Base case: n < 2 f(n) = 0

Recursion with a Different Base Case, cont.

Page 5: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Here is a sample implementation. The recursive method recursiveDivisionByTwo() is

typed int and takes an int parameter named dividend.

public static int recursiveDivisionByTwo(int dividend){ if(dividend < 2) return 0; else return 1 + recursiveDivisionByTwo(dividend – 2);}

Recursion with a Different Base Case, cont.

Page 6: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Building on this example, it is possible to write a recursive definition for the general division function.

This is shown on the next slide. The function has two parameters.

Adding 1 in the recursive case again counts how many times the recursion occurs.

In this definition the first parameter for the recursive case is dividend – divisor.

The amount decremented each time is always the same, but divisor can be any (positive integer) value.

The second parameter is the divisor, which is passed down unchanged through all of the calls.

Recursion with Two Parameters

Page 7: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Recursive case: dividend >= divisor f(dividend, divisor) = 1 + f(dividend –

divisor, divisor)   Base case: dividend < divisor f(dividend, divisor) = 0

Recursion with Two Parameters, cont.

Page 8: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Here is a complete driver program and an implementation of this function.

As usual, this is not a general implementation, but it works for appropriate input values and illustrates the key points.

Notice that a variable retVal is used in the implementation.

Recursion with Two Parameters, cont.

Page 9: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

import java.util.Scanner; public class GenRecDivProg{

public static void main(String[] args) {

Scanner in = new Scanner(System.in); int retVal; int dividend, divisor; System.out.print("Enter the dividend: "); dividend = in.nextInt(); System.out.print("Enter the divisor: "); divisor = in.nextInt(); retVal = GenRecDivClass.genRecDiv(dividend, divisor); System.out.println(retVal);

}}

Recursion with Two Parameters, cont.

Page 10: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

public class GenRecDivClass{ public static int genRecDiv(int dividend, int divisor) { int retVal = 0;  if(dividend < divisor) return retVal;  else { retVal = 1 + genRecDiv(dividend - divisor, divisor); return retVal; } }}

Recursion with Two Parameters, cont.

Page 11: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Just as division can be defined by repeated subtraction, finding a logarithm can be defined by repeated division.

For a given number to find the log of, which will be represented by the variable name toFindLogOf, and a given base, a simple recursive definition of the logarithm function would look like this:

Recursive case: toFindLogOf >= base f(toFindLogOf, base) = 1 + f(toFindLogOf / base, base)   Base case: toFindLogOf < base f(toFindLogOf, base) = 0

Recursion for a logarithm

Page 12: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

The following example contains two static methods, both of which find a simple logarithm by doing repeated division.

One does so with looping and the other with recursion.

For positive input consisting of a given double toFindLogOf and a given integer base, the code gives as output the largest integer value less than or equal to the logarithm for that number and base.

Recursion for a logarithm, cont.

Page 13: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

import java.util.Scanner; public class TestRecAndLoop{ public static void main(String[] args) { Scanner in = new Scanner(System.in);  double toFindLogOf; int base; double answer;  System.out.println("What number would you like to find the log of?"); toFindLogOf = in.nextDouble();  System.out.println("What should the base of the logarithm be?"); base = in.nextInt();  answer = RecursionAndLooping.myLogLooping(toFindLogOf, base);  System.out.println("The looping answer is: " + answer);  answer = RecursionAndLooping.myLogRec(toFindLogOf, base);  System.out.println("The recursive answer is: " + answer); }}

Recursion and looping for a logarithm, cont.

Page 14: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

public class RecursionAndLooping{ public static int myLogLooping(double toFindLogOf, int base) { int retVal = 0;  while(toFindLogOf >= base) { toFindLogOf = toFindLogOf / base; retVal++; } return retVal; }  public static int myLogRec(double toFindLogOf, int base) { int retVal = 0;  if(toFindLogOf < base) return retVal;  else { toFindLogOf = toFindLogOf / base; retVal = 1 + myLogRec(toFindLogOf, base); return retVal; } }}

Recursion and looping for a logarithm, cont.

Page 15: The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Note that the following version of the recursive method is shorter, but it might also be a little bit harder to understand. public static int myLogRec(double toFindLogOf, int base){ int retVal = 0;

if(toFindLogOf > base) { toFindLogOf = toFindLogOf / base; retVal = 1 + myLogRec(toFindLogOf, base); }

return retVal;}

Different Way for the Recursive Method