1 / 26

ITI 1120 Lab #9

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()

hedva
Download Presentation

ITI 1120 Lab #9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Lab 9 Agenda • Recursion • Examples of recursive algorithms • Introduction to matrices

  3. 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()

  4. 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.

  5. 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)

  6. Practice problem #1 – solution – cont’d BODY: A[N-1] ≥ ′0′ AND A[N-1] ′9′ ? false true false true N = 1 ? AllDigits  False AllDigits  CheckDigits(A, N-1) AllDigits  True

  7. Practice problem #1 –– cont’d • Translate the algorithm into a Java method

  8. 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; }

  9. Practice problem #2 • Write a recursive algorithm to test if a given array is in sorted order.

  10. 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)

  11. Practice problem #2 – solution – cont’d BODY: false true N = 1 ? false A[N–2] < A[N–1]? true Sorted  True Sorted  False Sorted  CheckSorted(A, N-1)

  12. 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.

  13. 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) FillArrayis the recursive algorithm

  14. 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)

  15. Practice problem #3 – solution – cont’d BODY: false N = 0 ? true FillArray(A, N-1)  A[N]  N

  16. 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 Aand Bare integers greater than or equal to 1.

  17. 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.

  18. Euclid’s Algorithm GIVENS: A, B(Two integers > 0) RESULT: GCD (greatest common divisor of A and B) HEADER: GCD  FindGCD(A, B)

  19. Euclid’s Algorithm BODY: false true A ≥ B ? false true GCD FindGCD(B, A) A MOD B = 0 ? GCD  B GCD FindGCD(B, A MOD B)

  20. Euclid’s Algorithm in Java public 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; }

  21. 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

  22. 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

  23. 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

  24. Matrix example Write an algorithm that finds the sum of the upper triangle of a square matrix (i.e. the diagonal and up). 0 1 2 3 4 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? 0 1 2 3 4 line_index <= column_index

  25. 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)

  26. BODY: Matrix example – cont’d SUM  0 R  0 true false R < N ? C  0 C < N ? true false R  C ? false true  SUM  SUM + M[R][C] R  R + 1 C  C + 1

More Related