30
ITI 1120 Lab #8 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams

ITI 1120 Lab #8

  • Upload
    mckile

  • View
    37

  • Download
    5

Embed Size (px)

DESCRIPTION

ITI 1120 Lab #8. Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams. Lab 8 Agenda. Recursion Examples of recursive algorithms Recursive Java methods. Recursion Practice problem #1. - PowerPoint PPT Presentation

Citation preview

Page 1: ITI 1120 Lab #8

ITI 1120Lab #8

Contributors: Diana Inkpen, Daniel Amyot, Sylvia

Boyd, Amy Felty, Romelia Plesa, Alan Williams

Page 2: ITI 1120 Lab #8

Lab 8 Agenda

• Recursion– Examples of recursive algorithms– Recursive Java methods

Page 3: ITI 1120 Lab #8

Recursion Practice problem #1

• Write a recursive algorithm for counting the number of digits in a non-negative integer, N

Page 4: ITI 1120 Lab #8

Practice problem #1- solution

GIVENS: N

INTERMEDIATE: RestOfDigits

RESULT: Count (the number of digits in N)

HEADER: Count NumberOfDigits(N)

Page 5: ITI 1120 Lab #8

Practice problem #1 – solution – cont’d

BODY:

RestOfDigits = N / 10

RestOfDigits = 0 ?

true

Count 1

false

Count NumberOfDigits(RestOfDigits)Count Count + 1

Page 6: ITI 1120 Lab #8

Trace for N = 254

Line N RestOfDigits Count

Initial values 254 ? ?

(1) RestOfDigits = N / 10 25

(2) RestOfDigits = 0 ? false

(3) CALL Count NumberOfDigits(RestOfDigits)

(4) Count Count + 1

Page 7: ITI 1120 Lab #8

Trace, page 2

Line N RestOfDigits Count

Initial values 25 ? ?

(1) RestOfDigits = N / 10 2

(2) RestOfDigits = 0 ? false

(3) CALL Count NumberOfDigits(RestOfDigits)

(4) Count Count + 1

Count NumberOfDigits(RestOfDigits)

Count NumberOfDigits(N)25

Page 8: ITI 1120 Lab #8

Trace , page 3

Line N RestOfDigits Count

Initial values 2 ? ?

(1) RestOfDigits = N / 10 0

(2) RestOfDigits = 0 ? true

(5) Count 1 1

Count NumberOfDigits(RestOfDigits)

Count NumberOfDigits(N)21

Page 9: ITI 1120 Lab #8

Trace , page 2

Line N RestOfDigits Count

Initial values 25 ? ?

(1) RestOfDigits = N / 10 2

(2) RestOfDigits = 0 ? false

(3) CALL Count NumberOfDigits(RestOfDigits)

1

(4) Count Count + 1 2

Count NumberOfDigits(RestOfDigits)

Count NumberOfDigits(N)252

Page 10: ITI 1120 Lab #8

Trace, page 1

Line N RestOfDigits Count

Initial values 254 ? ?

(1) RestOfDigits = N / 10 25

(2) RestOfDigits = 0 ? false

(3) CALL Count NumberOfDigits(RestOfDigits)

2

(4) Count Count + 1 3

Page 11: ITI 1120 Lab #8

Implement this algorithm in Java

Page 12: ITI 1120 Lab #8

Let’s see how the code runs

• In your recursive method:• Put System.out.println() statements at the following

locations (print the actual value of n):– Immediately after local variable declarations

1: Entering method with n = 254– Just before recursive method call:

2: Recursive call from n = 254– Just after recursive method call:

3: Returned from recursion with n = 254– Just before return statement

4: Returning from method with n = 254, count = 3– In the base case

5: Base case with n = 2

Page 13: ITI 1120 Lab #8

Practice problem #2

• Write a recursive algorithm to test if all the characters in positions 0...N-1 of an array, A, of characters are digits.

Page 14: ITI 1120 Lab #8

Practice problem #2 - solution

GIVENS:

A (an array of characters)

N (test up to this position in array)

RESULT:

AllDigits (Boolean, true if all characters in position 0..N are digits)

HEADER:

AllDigits CheckDigits(A,N)

Page 15: ITI 1120 Lab #8

Practice problem #2 – solution – cont’d

BODY:

A[N-1] ≥ ′0′ AND A[N-1] ′9′ ?

true

N = 1 ?true

AllDigits True

false

AllDigits CheckDigits(A, N-1)

false

AllDigits False

Page 16: ITI 1120 Lab #8

Practice problem #2 –– cont’d

• Translate the algorithm into a Java method

Page 17: ITI 1120 Lab #8

Practice problem #2 – solution – cont’d

Page 18: ITI 1120 Lab #8

Practice problem #3

• Write a recursive algorithm to test if a given array is in sorted order.

Page 19: ITI 1120 Lab #8

Practice problem #3 - solution

GIVENS:

A (an array of integers)

N (the size of the array)

RESULT:

Sorted (Boolean: true if the array is sorted)

HEADER:

Sorted CheckSorted(A,N)

Page 20: ITI 1120 Lab #8

Practice problem #3 – solution – cont’d

BODY:

A[N–2] < A[N–1] ?

true

N = 1 ?true

Sorted True

false

Sorted CheckSorted(A, N-1)

false

Sorted False

Page 21: ITI 1120 Lab #8

Practice problem #4

• Write a recursive algorithm to create an array containing the values 0 to N-1

• Hint:– Sometimes you need 2 algorithms:

• The first is a “starter” algorithm that does some setup actions, and then starts off the recursion by calling the second algorithm

• The second is a recursive algorithm that does most of the work.

Page 22: ITI 1120 Lab #8

Practice problem #4 - solution

GIVENS:

N (the size of the array)

RESULT:

A (the array)

HEADER:

A CreateArray(N)

BODY:

A MakeNewArray(N)

FillArray(A, N – 1)

FillArray is the recursive algorithm

Page 23: ITI 1120 Lab #8

Practice problem #4 – solution – cont’d

Algorithm FillArray

GIVENS:

A (an array)

N (the largest position in the array to fill)

MODIFIEDS:

A

RESULT:

(none)

HEADER:

FillArray(A, N)

Page 24: ITI 1120 Lab #8

Practice problem #4 – solution – cont’d

BODY:

N = 0 ? true

false

FillArray(A, N-1)

A[N] N

Page 25: ITI 1120 Lab #8

Practice Problem #5: Euclid’s algorithm

• The greatest common divisor (GCD) of two positive integers is the largest integer that divides both values with remainders of 0.

• Euclid’s algorithm for finding the greatest common divisor is as follows:gcd(a, b) is …

• b if a ≥ b and a mod b is 0

• gcd(b, a) if a < b

• gcd(b, a mod b) otherwise

• Write a recursive algorithm that takes two integers A and B and returns their greatest common divisor. You may assume that A and B are integers greater than or equal to 1.

Page 26: ITI 1120 Lab #8

What is the base case?

FindGCD(A, B) is …• B if A ≥ B and A MOD

B is 0• FindGCD(B, A) if A < B

• FindGCD(B, A MOD B) otherwise

• Question: will this algorithm always reach the base case?– Note that A MOD B is at most B – 1.

Page 27: ITI 1120 Lab #8

Euclid’s Algorithm

GIVENS:

A, B (Two integers > 0)

RESULT:

GCD (greatest common divisor of A and B)

HEADER:

GCD FindGCD(A, B)

Page 28: ITI 1120 Lab #8

Euclid’s AlgorithmBODY:

A MOD B = 0 ?true

GCD B

false

GCD FindGCD(B, A MOD B)

A ≥ B ?

GCD FindGCD(B, A)

truefalse

Page 29: ITI 1120 Lab #8

Euclid’s Algorithm in Java

Page 30: ITI 1120 Lab #8

Test this method

• Create a class Euclid that includes the method findGCD, and also a main( ) method that will ask the user to enter two values and print their GCD.

• Try the following:a = 1234, b = 4321

a = 8192, b = 192