CS1101X: Programming Methodology Recitation 9 Recursion II.

Slides:



Advertisements
Similar presentations
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming Methodology (1). Implementing Methods main.
Backtracking What is backtracking?
Maths for Computer Graphics
Swap I class Swap { public static void swap(int i, int j) {
What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion!. Can a method call another method? YES.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Computer Programming Lab(5).
CS1101X: Programming Methodology Recitation 7 Arrays II.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 CSE 142 Final Exam Review Problems. 2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
CS1101X: Programming Methodology Recitation 6 Arrays I.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
Chapter 2 Clarifications
Lecture 9: introduction to recursion reading: 12.1
תרגול 3 שיטות ומערכים.
Recursion -- Introduction
Building Java Programs
Department of Computer Science
Recursion Review Mr. Jacobs.
Sum of natural numbers class SumOfNaturalNumbers {
Introduction to Recursion - What is it? - When is it used? - Examples
Recursion
CS1101X: Programming Methodology Recitation 8 Recursion I
Java Software Structures: John Lewis & Joseph Chase
Case Study 2 – Marking a Multiple-choice Test
slides adapted from Marty Stepp and Hélène Martin
Chapter 12 Recursion (methods calling themselves)
Computing Adjusted Quiz Total Score
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
The Basics of Recursion
Recursive GCD Demo public class Euclid {
slides created by Marty Stepp and Alyssa Harding
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Factoring if/else code
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
slides adapted from Marty Stepp and Hélène Martin
Recursion Method calling itself (circular definition)
Factoring if/else code
slides created by Marty Stepp
Building Java Programs
Presentation transcript:

CS1101X: Programming Methodology Recitation 9 Recursion II

CS1101X Recitation #92 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?

CS1101X Recitation #93 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 > 0 public 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; }

CS1101X Recitation #94 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.

CS1101X Recitation #95 Task 1: Mystery (4/6) “Going-up” version

CS1101X Recitation #96 Task 1: Mystery (5/6) “Split-half” version

CS1101X Recitation #97 Task 1: Mystery (6/6) Other version

CS1101X Recitation #98 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.

CS1101X Recitation #99 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 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].

CS1101X Recitation #910 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.

CS1101X Recitation #911 Task 2: Transpose Matrix (4/6) createMatrix

CS1101X Recitation #912 Task 2: Transpose Matrix (5/6) printMatrix

CS1101X Recitation #913 Task 2: Transpose Matrix (6/6) transpose

CS1101X Recitation #914 End of Recitation #9