moar_practice_ans

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

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

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 specifies 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. Furthermore, 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 Fibonacci number is equal to the sum of the two numbers in the Fibonacci sequence that precede it. Let F n denote the n th Fibonacci number. Then F n = F n 2 + F n 1 . The first eight Fibonacci numbers are 0 , 1 , 2 , 3 , 5 , 8 , and 13 . Write a function int fib(int n) that returns the n th Fibonacci number. For 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) 2 { 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

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

View Full Document
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 finds 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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern