ITI 1120 Lab #9

26
ITI 1120 Lab #9 Contributors: Diana Inkpen, Daniel Amyot, Romelia Plesa, Alan Williams

description

ITI 1120 Lab #9. Contributors: Diana Inkpen, Daniel Amyot, Romelia Plesa, Alan Williams. Lab 9 Agenda. Recursion Examples of recursive algorithms Introduction to matrices. Reading arrays from the keyboard. Use the provided ITI1120 class: ITI1120.readIntLine() - PowerPoint PPT Presentation

Transcript of ITI 1120 Lab #9

Page 1: ITI 1120 Lab #9

ITI 1120Lab #9

Contributors: Diana Inkpen, Daniel Amyot, Romelia

Plesa, Alan Williams

Page 2: ITI 1120 Lab #9

Lab 9 Agenda

• Recursion– Examples of recursive algorithms– Introduction to matrices

Page 3: ITI 1120 Lab #9

Reading arrays from the keyboard

• Use the provided ITI1120 class:

ITI1120.readIntLine()• Reads an array of integers• When entering the values, be sure to leave at least one space between

them.• The result is of type int[]• You should ask for the length of the array that is returned, to

find out its size.

ITI1120.readDoubleLine()• Similar to readIntLine(), except for double values

ITI1120.readCharLine()• Reads an array of character values• Includes ALL characters, including spaces• Result is of type char[]

• Reading a String ITI1120.readString()

Page 4: ITI 1120 Lab #9

Practice problem #1

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

Page 5: ITI 1120 Lab #9

Practice problem #1 - 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 6: ITI 1120 Lab #9

Practice problem #1 – 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 7: ITI 1120 Lab #9

Practice problem #1 –– cont’d

• Translate the algorithm into a Java method

Page 8: ITI 1120 Lab #9

Practice problem #1 – solution – cont’d

public static boolean checkDigits( char[] a, int n ) { boolean allDigits; if( a[n-1] >= '0' && a[n-1] <= '9' ) { if ( n == 1 ) { allDigits = true; } else { allDigits = checkDigits( a, n-1 ); } } else { allDigits = false; } return allDigits; }

Page 9: ITI 1120 Lab #9

Practice problem #2

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

Page 10: ITI 1120 Lab #9

Practice problem #2 - 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 11: ITI 1120 Lab #9

Practice problem #2 – 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 12: ITI 1120 Lab #9

Practice problem #3

• 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 13: ITI 1120 Lab #9

Practice problem #3 - 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 14: ITI 1120 Lab #9

Practice problem #3 – 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 15: ITI 1120 Lab #9

Practice problem #3 – solution – cont’d

BODY:

N = 0 ? true

false

FillArray(A, N-1)

A[N] N

Page 16: ITI 1120 Lab #9

Practice Problem #4: 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 17: ITI 1120 Lab #9

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 18: ITI 1120 Lab #9

Euclid’s Algorithm

GIVENS:

A, B (Two integers > 0)

RESULT:

GCD (greatest common divisor of A and B)

HEADER:

GCD FindGCD(A, B)

Page 19: ITI 1120 Lab #9

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 20: ITI 1120 Lab #9

Euclid’s Algorithm in Javapublic static int findGCD( int a, int b ){ int gcd; if ( a >= b ) { if ( a % b == 0 ) { gcd = b; } else { gcd = findGCD( b, a % b ); } } else { gcd = findGCD( b, a ); } return gcd;}

Page 21: ITI 1120 Lab #9

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

Page 22: ITI 1120 Lab #9

Matrices

• A matrix is a two dimensional rectangular grid of numbers:

• The dimensions of the matrix are the numbers of rows and columns (in the above case: row dimension 3, column dimension 3).

• A value within a matrix is referred to by its row and column indices, in that order.– Math: number rows and columns from 1, from upper left corner

• In math notation, M1,2 = 2– For algorithms (and Java), we will use indices starting from 0, to

correspond to the array numbering.• In algorithm notation, M[0][1] = 2

987

654

321

M

Page 23: ITI 1120 Lab #9

Matrix element processing

• To visit every element of an array, we had to use a loop.

• To visit every element of a matrix, we need to use a loop inside a loop:– Outer loop: go through each row of a matrix– Inner loop: go through each column within

one row of a matrix.

• Recursion could be used in place of each loop

Page 24: ITI 1120 Lab #9

Matrix example

Write an algorithm that finds the sum of the upper triangle of a square matrix (i.e. the diagonal and up).

1 4 5 3 2

6 3 6 4 6

M = 4 3 6 7 2

3 4 2 2 4

2 3 8 3 5

How do we know if an element of a square matrix is on or above the main diagonal?

line_index <= column_index

0 1 2 3 4

0

1

2

3

4

Page 25: ITI 1120 Lab #9

Matrix example – cont’d

GIVENS:

M (the matrix)

N (the number of rows and columns in the matrix)

RESULT:

Sum (the sum of all elements in the upper triangle)

INTERMEDIATES:

R (row index in the matrix)

C column index in the matrix)

HEADER:

Sum CalculateUpperTriangle(M,N)

Page 26: ITI 1120 Lab #9

Matrix example – cont’dBODY: SUM 0

R 0

R < N ?true

C < N ? true

R C ? true

SUM SUM + M[R][C]

C C + 1

false

C 0

false

R R + 1

false