CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
CS1101X: Programming Methodology Recitation 9 Recursion II.
-
Upload
caroline-robinson -
Category
Documents
-
view
214 -
download
2
Transcript of CS1101X: Programming Methodology Recitation 9 Recursion II.
![Page 1: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/1.jpg)
CS1101X: Programming Methodology
Recitation 9 Recursion II
![Page 2: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/2.jpg)
CS1101X Recitation #9 2
Task 1: Mystery (1/6)
Study the code below and if you like, trace the recursive method mystery(). What is the smaller version of the task on which the recursive call works? How does the original problem relate to the smaller problem? What does the method compute?
![Page 3: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/3.jpg)
CS1101X Recitation #9 3
Task 1: Mystery (2/6)public static void main(String[] args) { // code to read values into array - omitted System.out.print("Answer = " + mystery(list, list.length); }}
// pre-cond: n > 0public static int mystery(int[] a, int n) { if (n == 1) return a[0]; else { int m = mystery(a, n-1); return a[n-1] > m ? a[n-1]: m; }}
![Page 4: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/4.jpg)
CS1101X Recitation #9 4
Task 1: Mystery (3/6)
Is the code a “going-up”, “going-down” or “split-half” recursion?
Write the other two versions. You may also try other versions.
![Page 5: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/5.jpg)
CS1101X Recitation #9 5
Task 1: Mystery (4/6)“Going-up” version
![Page 6: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/6.jpg)
CS1101X Recitation #9 6
Task 1: Mystery (5/6)“Split-half” version
![Page 7: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/7.jpg)
CS1101X Recitation #9 7
Task 1: Mystery (6/6)Other version
![Page 8: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/8.jpg)
CS1101X Recitation #9 8
Task 2: Transpose Matrix (1/6)
To transpose a square matrix, the columns become the rows and the rows become columns.
For example, the 55 matrix on the left below is transposed into the matrix on the right.
716345315
31530518
12224126
83111020
2398414
73112823
1652239
343041118
5352104
151862014
![Page 9: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/9.jpg)
CS1101X Recitation #9 9
Task 2: Transpose Matrix (2/6)
Write a recursive method to transpose a square matrix.
Hint: If the original problem is an nn matrix that extends from m[0][0] to m[n-1][n-1], then the smaller problem is the (n-1)(n-1) matrix that extends from m[0][0] to m[n-2][n-2].
![Page 10: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/10.jpg)
CS1101X Recitation #9 10
Task 2: Transpose Matrix (3/6)import java.util.*;
class Transpose { public static void main(String[] args) { int[][] matrix = createMatrix();
System.out.println("Before transpose:"); printMatrix(matrix);
transpose(matrix, matrix.length);
System.out.println(); System.out.println("After transpose:"); printMatrix(matrix); } . . .}
Write the methods createMatrix, printMatrix, and transpose.
![Page 11: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/11.jpg)
CS1101X Recitation #9 11
Task 2: Transpose Matrix (4/6)createMatrix
![Page 12: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/12.jpg)
CS1101X Recitation #9 12
Task 2: Transpose Matrix (5/6)printMatrix
![Page 13: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/13.jpg)
CS1101X Recitation #9 13
Task 2: Transpose Matrix (6/6)transpose
![Page 14: CS1101X: Programming Methodology Recitation 9 Recursion II.](https://reader036.fdocuments.net/reader036/viewer/2022072009/56649da05503460f94a8c599/html5/thumbnails/14.jpg)
CS1101X Recitation #9 14
End of Recitation #9