CS1101X: Programming Methodology Recitation 8 Recursion I

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Computer Programming Lab(7).
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion.
CS1101X: Programming Methodology Recitation 9 Recursion II.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Computer Programming Lab(5).
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
CS1101X: Programming Methodology Recitation 3 Control Structures.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.
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.
Recursion.
Planning & System installation
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Recursion DRILL: Please take out your notes on Recursion
Recursion 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
AKA the birth, life, and death of variables.
Recursion Chapter 12.
More on Recursion.
Principles of programming languages 4: Parameter passing, Scope rules
Object Oriented Systems Lecture 03 Method
Java Programming: Program Design Including Data Structures
Recursion Chapter 10.
CIS Principles of Programming
C++ Arrays.
User-Defined Functions
Announcements Final Exam on August 17th Wednesday at 16:00.
User-defined Functions
ICS 353: Design and Analysis of Algorithms
The for-loop and Nested loops
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Stack Memory 2 (also called Call Stack)
Programming application CC213
User-defined Functions
Recursion Recursion is a math and programming tool
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
ICS 353: Design and Analysis of Algorithms
Introduction to Programming
Java Lesson 36 Mr. Kalmes.
Parameter Passing in Java
ICS 353: Design and Analysis of Algorithms
AKA the birth, life, and death of variables.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Programming with Recursion
Recursion Problems.
Recursion Based on slides by Alyssa Harding
Recursion yet another look To recurse is divine, to iterate is human
slides created by Alyssa Harding
Programming with Recursion
Self-Referencing Functions
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
各題答對人數.
Recursive example 1 double power(double x, int n) // post: returns x^n
CS100A Sections Dec Loop Invariant Review C Review and Example
slides created by Alyssa Harding
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
ICS 353: Design and Analysis of Algorithms
Divide two Integers.
Presentation transcript:

CS1101X: Programming Methodology Recitation 8 Recursion I

Task 1: Tracing Recursion #1 (1/3) Trace the following recursive methods using various input data. Restrict your data to non-negative integers. From your results, describe in words what P() and R() do. If you interchange the two lines in method P() and call it P1(), what does P1() do? CS1101X Recitation #8

Task 1: Tracing Recursion #1 (2/3) // pre-cond: n >= 0 public static void P(int n) { if (n < 10) System.out.print(n); else { P(n/10); // what if you swap System.out.print(n % 10); // these two lines } // pre-cond: n >= 0 public static void R(int n) { System.out.print(n % 10); if (n/10 != 0) R(n/10); } CS1101X Recitation #8

Task 1: Tracing Recursion #1 (3/3) Trace the methods for n = 3792 For P : 3792 For P1: 2973 For R : 2973 CS1101X Recitation #8

Task 2: Tracing Recursion #2 (1/4) Given the following method Q(). public static void Q(int n1, int n2) { if (n2 <= 0) System.out.println(); else { Q(n1 – 1, n2 – 1); System.out.print(n1 + " "); Q(n1 + 1, n2 – 1); } CS1101X Recitation #8

Task 2: Tracing Recursion #2 (2/4) What output is produced by the call Q(14,3)? [Hint: First try Q(14,0), then Q(14,1), then Q(14,2).] If the System.out.print(n1 + " ") statement is moved before the first recursive call to Q(), what output will be produced by Q(14,3)? Write out a complete program to test this out. CS1101X Recitation #8

Task 2: Tracing Recursion #2 (3/4) Write your outputs here. 12 13 14 15 16 CS1101X Recitation #8

Task 2: Tracing Recursion #2 (4/4) Tracing Q(14,3) Q(16, 1) Q(17, 0) Q(15, 2) Q(15, 0) Q(13, 0) Q(11, 0) Q(14, 1) Q(12, 1) Q(13, 2) Q(14, 3) println “13” “12” “14” “15” “16” CS1101X Recitation #8

Task 3: Power method (1/2) The Math class provides the method pow(double a, double b) that returns the value of the first parameter raised to the power of the second parameter. Write your own recursive method power(double x, int n) to compute xn, where n is a non-negative integer. For example, power(3.0, 4) = 3.0*3.0*3.0*3.0 = 81.0; and power(2.5, 3) = 2.5*2.5*2.5 = 15.625. Test out your power() method and compare the results with the pow() method. CS1101X Recitation #8

Task 3: Power method (2/2) Write your method here. // pre-cond: n >= 0 public static double power(double x, int n) { if (n == 0) return 1.0; else return x * power(x, n-1); } CS1101X Recitation #8

Task 4: Lining up children (1/2) You need to line up n children in a queue. According to some unwritten rule, a girl may stand next to another girl or a boy, but two boys must not stand next to each other. Therefore, there are only 3 ways for a line of 2 children to be formed: boy-girl, girl-boy, and girl-girl. Write a recursive method lineup(int n) to compute the number of ways a line of n children may be formed. CS1101X Recitation #8

Task 4: Lining up children (2/2) Write your method here. // pre-cond: n>= 1 public static int lineup(int n) { if (n == 1) return 2; else if (n == 2) return 3; else return lineup(n-1) + lineup(n-2); } CS1101X Recitation #8

Next week… We will do recursion on data structures (arrays). CS1101X Recitation #8

End of Recitation #8 CS1101X Recitation #8