moar_practice_ans

moar_practice_ans - 5 A Last-minute Introduction to...

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

View Full Document Right Arrow Icon
5 A Last-minute Introduction to Recursion A function that invokes itself is a recursive function. Consider the recursive factorial function below, which computes the value of the expression n ! : 1 int factorial( int n) 2{ 3 if (n <= 0) // Base case: 0! is 1 by definition; we don’t worry about negative values of n 4 return 1; 5 else 6 return n * factorial(n - 1); // Recursive case: n! = n * (n-1)! 7} This function works as we might expect; factorial(0) returns 1, factorial(1) returns 1, factorial(3) returns 6, and factorial(5) returns 120. A recursive function usually has an if block with a recursive case that invokes itself and a base case that speciFes where to stop. A through treatment of recursion is be- yond the scope of this practice exam. The important idea to note here is that it’s perfectly OK for a function to call itself. ±urthermore, we can use an if condition to control whether the function repeatedly calls itself or returns a base case value instead. 5.1 Comprehending Recursion What does factorial(6) return? Answer: 720 How many calls to factorial does the expression factorial(8) cause? Answer: 9 calls. One call for positive numbers 1 to 8 and one call for factorial(0) , which returns 1. 5.2 Recursive Fibonacci Function Let’s now write a recursive function. Consider the sequence of Fibonacci numbers where the n th ±ibonacci number is equal to the sum of the two numbers in the ±ibonacci sequence that precede it. Let F n denote the n th ±ibonacci number. Then F n = F n 2 + F n 1 . The Frst eight ±ibonacci numbers are 0 , 1 , 2 , 3 , 5 , 8 , and 13 . Write a function int fib(int n) that returns the n th ±ibonacci number. ±or example, the expression fib(9) returns 21 and the expression fib(11) returns 55. You can write the function in 6 lines of code. 1 int fib( int n) 3 if (n == 1) 4 return 0; 5 else if (n == 2) 6 return 1; 7 else 8 return fib(n - 2) + fib(n - 1); 9} 9
Background image of page 1

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

View Full DocumentRight Arrow Icon
6 More Classes, Structs, and Recursion (Oh My!) Part 1 Mark Zuckerberg (the CEO of Facebook) has asked you to help with a new Facebook App that stores peo- ple’s family trees. For each person who installs the App, the App ±nds that person’s parents, and the parents’ parents, etc, and creates that person’s family tree. Mark says the App will revolutionize the way people com- municate. The App has not yet been completed, but an intern started on the App last year. To begin, you must write a data structure to store each user and the relationships between users. The intern who worked on this project last summer wrote a small amount of prototype code but then had to leave the project to compete in a StarCraft tournament. The (completely undocumented) code is below: 1 struct TreeNode 2{ 3 TreeNode() 4 : left(NULL), right(NULL) 5{ } 6 7 TreeNode * left; 8 TreeNode * right; 9 string name; 10 11 void printPreOrder() const ; 12 void printPostOrder() const ; 13 void printInOrder() const ; 14 } 15 16 int main() 17 { 18 TreeNode a; 19 TreeNode b; 20 TreeNode c; 21 TreeNode d; 22 TreeNode e; 23 24 c.name = "Copernicus"; 25 b.name = "Bart"; 26 e.name = "Ed"; 27 a.name = "Alice"; 28 d.name = "Dylan"; 29 30
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 8

moar_practice_ans - 5 A Last-minute Introduction to...

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

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