day8 - COP 3503 Computer Science II CLASS NOTES DAY#8...

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

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #8 More on Recursion Let’s look at a couple recursive static methods in Java that calculate Fibonacci and Factorials, respectively: Day 8 - 1 Chapter 7 public static int Fibonacci(int n) { if (n < 0) return –1; // This signifies an illegal input parameter. else if (n < 2) return n; else return Fibonacci(n-1) + Fibonacci(n-2); } public static int Factorial(int n) { if (n < 0) return –1; // This signifies an illegal input parameter. else if (n < 2) return 1; else return n*Factorial(n-1);
Background image of page 1

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

View Full Document Right Arrow Icon
Here is a recursive binary search: Consider the following problem: Given a positive integer value of cents, how many different ways can we make change equaling that number of cents, using only pennies, nickels, dimes, and quarters. Here is a recursive solution: Day 8 - 2 public static int Search(int[] X, int low, int high, int value) { if (low > high) return -1; else { int mid = (low + high)/2; if (value > X[mid]) return Search(X,mid+1,high,value); else if (value < X[mid]) return Search(X,low,mid-1,value); else return mid; } } public static int Change(int cents, int den) { if (cents < 0) return 0; else if (den == 1 || cents == 0) return 1; else { int sum = 0; switch(den) { case 25: sum+=Change(cents-25,25); case 10: sum+=Change(cents-10,10); case 5: sum+=Change(cents-5,5); case 1: sum+=Change(cents-1,1); } return sum; } }
Background image of page 2
There is A LOT going on in this short piece of code. The essential idea is the following: If you have n cents, you could make change for it by doing the following: 1) use a quarter, then count how many ways to change n-25 cents 2) use a dime, then count how many ways to change n-10 cents 3) use a nickel, then count how many ways to change n-5 cents 4) use a penny, then count how many ways to change n-1 cents But, there’s a problem here – what is it? So we can take care of this problem with the following stipulations: 1) use a quarter, then count how many ways to change n-25 cents 2) use a dime, then count the ways to change n-10 cents, with dimes or less 3) use a nickel, then count the ways to change n-5 cents, with nickels or less 4) use a penny, then count the ways to change n-1 cents, with pennies or less Why does this take care of our problem? Essentially, in our function, we return the number of ways to make change for cents cents, where den is our largest denomination. THUS, the number of recursive calls we make DEPENDS on our value of DEN. If DEN=5, then we only need to make 2 calls, if it’s 10 we need to make 3, etc. The sum of these recursive calls is the answer to our question. Notice, how we add the values returned by the appropriate number of recursive calls using the switch statement WITHOUT breaks. You can certainly implement this algorithm without a switch statement, but then the code would be slightly more cumbersome. Also, we need to take a look at our “base” cases so to speak. A recursive function CAN NOT call itself always. (Why is this?) So, when the problem is easy enough to solve on it’s own right, you can just directly solve the problem. In essence, as was mentioned in CS1, you do one of two things: 1) Solve the problem because it’s simple.
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 9

day8 - COP 3503 Computer Science II CLASS NOTES DAY#8...

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

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