CS1101X: Programming Methodology Recitation 9 Recursion II - PowerPoint PPT Presentation

About This Presentation
Title:

CS1101X: Programming Methodology Recitation 9 Recursion II

Description:

To transpose a square matrix, the columns become the rows and the rows become columns. ... Task 2: Transpose Matrix (4/6) createMatrix. CS1101X Recitation #9. 12 ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 15
Provided by: aaro3
Category:

less

Transcript and Presenter's Notes

Title: CS1101X: Programming Methodology Recitation 9 Recursion II


1
CS1101X Programming MethodologyRecitation 9
Recursion II
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?
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 gt 0 public static int mystery(int
a, int n) if (n 1) return a0 else
int m mystery(a, n-1) return
an-1 gt m ? an-1 m
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.
5
Task 1 Mystery (4/6)
Going-up version
6
Task 1 Mystery (5/6)
Split-half version
7
Task 1 Mystery (6/6)
Other version
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 5?5 matrix on the left below is
transposed into the matrix on the right.
9
Task 2 Transpose Matrix (2/6)
Write a recursive method to transpose a square
matrix. Hint If the original problem is an n?n
matrix that extends from m00 to mn-1n-1,
then the smaller problem is the (n-1)?(n-1)
matrix that extends from m00 to mn-2n-2.
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.
11
Task 2 Transpose Matrix (4/6)
createMatrix
12
Task 2 Transpose Matrix (5/6)
printMatrix
13
Task 2 Transpose Matrix (6/6)
transpose
14
End of Recitation 9
Write a Comment
User Comments (0)
About PowerShow.com