05-Recursion - Quiz 1 Solution CS/ENGRD 2110 Object-Oriented Programming and Data Structures What IDE does the CS 2110 Staff recommend using for

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2010 Thorsten Joachims Lecture 5: Recursion Quiz 1 Solution What IDE does the CS 2110 Staff recommend using for this class? (a) Eclipse (b) Dr. Java (c) NetBeans (d) Emacs 2. Integer num1 = new Integer(2110); Integer num2 = new Integer(2110); System.out.println((num1 == num2) + ", " + (num1.equals(num2))); What is the output of the previous code? (a) "true, true" (b) "true, false" (c) "false, true" (d) "false, false" 3. Animal parrot = new Bird(); What is the static type of the field above? (a) Bird (b) Object (c) Parrot (d) Animal 4. Which is the correct google group for this class? (a) cornel -cs2110 (b) cornel -cs3110-sp11 (c) cornel -cs2110-sp11 (d) cornel -cs2110-sp10 2 Recursion Overview Recursion is a powerful technique for specifying functions, sets, and programs Example recursively-defined functions and programs factorial combinations exponentiation (raising to an integer power) solution of combinatorial problems (i.e. search) Example recursively-defined sets grammars expressions data structures (lists, trees, . ..) 3 The Factorial Function (n!) Define: n! = n·(n 1)·(n 2)···3·2·1 read : “n factorial” E.g., 3! = 3·2·1 = 6 The function int int that gives n! on input n is called the factorial function n! is the number of permutations of n distinct objects There is just one permutation of one object. 1! = 1 There are two permutations of two objects: 2! = 2 1 2 2 1 There are six permutations of three objects: 3! = 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 4 5 Permutations of Permutations of non- orange blocks Each permutation of the three non-orange blocks gives four permutations when the orange block is included Total number = 4·6 = 24 = 4! General: 0! = 1 (by convention) If n > 0, n! = n·(n-1)! A Recursive Program 6 static int fact(int n) { if (n = = 0) return 1; else return n*fact(n-1); } Recursive definition of n! • 0! = 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/17/2011 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 5

05-Recursion - Quiz 1 Solution CS/ENGRD 2110 Object-Oriented Programming and Data Structures What IDE does the CS 2110 Staff recommend using for

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online