![]() |
CPSC 211 |
|
Objectives
Part 1In this part of the lab we're going to warm up by tracing an existing
recursive method and then modify it to do slightly different tasks.
Begin by tracing the following recursive method (you can use a recursion tree
or droids to do this) in order to determine the output to the screen when the call
public class PrintUtil {
public static void print( int num ) {
if( num > 0 ) {
System.out.println( num % 10 );
print( num / 10 );
}
}
}
Now add a recursive public static method printForwards to the PrintUtil class. This method takes an integer as input
and prints the digits in that integer from left to right, one digit per line.
So, for example, the call printForwards(5702) should generate
the following output:
5 7 0 2 Now write a PrintUtilDriver class - (i.e., a fairly simple driver program with a main method) that you can use to test that your method works correctly. Now add to the PrintUtil class a recursive public static method printBackwardsForwards that takes an integer as input and prints the digits in that integer both backwards and forwards, one digit per line. So, for example, the call printBackwardsForwards(5702) should generate the following output: 2 0 7 5 7 0 2 Note that the first digit in the number (5 in this case) is printed only once! Modify your driver program to test the methodprintBackwardsForwards. Part 2Recall the following
public class PrintUtil {
public static void print( int num ) {
if( num > 0 ) {
System.out.println( num % 10 );
print( num / 10 );
}
}
}
Write the same method using iteration instead of recursion. This type of methods are called tail-recursive methods because the recursive call is the last statement executed by the recursive step of that method. Can you think of a systematic way of translating tail-recursive methods to iterative methods? Part 3In the last part of the lab, you will write a recursive method of the Domino class to output all pair-numbers of a set of dominos where the maximumNumberOfDots on a domino is user specified. Recall that a domino is a rectangle whose face is divided into two equal parts that are blank or bear from one to six dots (assuming the maximumNumberOfDots is 6 in this case). A set of dominos usually contains 28 dominos; that is, all the possible pair-combinations of the numbers from 0 to 6 with no repetition. This means that the pairs (1, 6) and (6, 1), for example, are not distinguished and the set contains only one of them. The header of the domino method is as follows: public static void printDominoSet( int dotsOnLeftSide, int dotsOnRightSide ) { ... } where dotsOnLeftSide and dotsOnRightSide are the number of dots on the left and the right-hand side of a domino, respectively.For example, the call Domino.printDominoSet( 4, 4 ); should produce the following output: 4 : 4 4 : 3 4 : 2 4 : 1 4 : 0 3 : 3 3 : 2 3 : 1 3 : 0 2 : 2 2 : 1 2 : 0 1 : 1 1 : 0 0 : 0 Hints:
Practice Section Only (not for marks)Practice Part 1Consider the Fibonacci method we discussed in the lectures: public int fib( int n ) { if( n == 1 || n == 2 ) return 1; else return fib( n – 1 ) + fib( n – 2 ); } This function is not tail-recursive as the first recursive call is not the last statement that is executed before the function calls itself. We still can define this method iteratively, but we need to think harder to do so. Your task in this part is to find an iterative definition for this function.
Practice Part 2To appreciate the expressive power of recursion we ask you to consider solving the well known problem of the Tower of Hanoi. The problem goes like the following:Initially there are 3 pegs, call them peg1, peg2, peg3, and n disks of different size stacked in decreasing size (smaller at the top) on one of the begs, say peg1. The goal of the game is to move all the discs (the tower) from peg1 to peg3 (using peg 2), moving one disk at a time, with the following restriction: a disk may never be placed on a disk that is smaller than itself. The following site has a nice animation of the problem: http://www.mazeworks.com/hanoi/index.htm Try a few times the animation with a different number of disks and speeds. Then define a function that if you give it the 3 pegs and the number of disks stored in the first peg, will print out the moves you need to make to move all the disks to peg 3. When you're done, find a solution to this problem on the Internet and compare your code with it.
Practice Part 3In this part of the lab you are to write a program that prompts the user for the number of rows in Pascal's triangle, and prints the triangle on the screen. One of the uses of Pascal's triangle is to determine the coefficients in the polynomial (a + b)n. The first few rows of Pascal's triangle are as follows:
1 - row 0 1 1 - row 1 1 2 1 - row 2 1 3 3 1 - row 3 1 4 6 4 1 - row 4 1 5 10 10 5 1 - row 5
Now note that: ( a + b )3 = 1a3 + 3a2b + 3ab2 + 1b3 - underlined coefficients match the numbers on row 3 of the triangle ( a + b )4 = 1a4 + 4a3b + 6a2b2 + 4ab3 + 1b4 - underlined coefficients match the numbers on row 4 of the triangle ( a + b )5 = 1a5 + 5a4b + 10a3b2 + 10a2b3 + 5ab4 + 1b5 - underlined coefficients match the numbers on row 5 of the triangle The entries in Pascal's triangle can be generated by adding together the 2 adjacent numbers in the previous row and noting that the first and last numbers on each row are always 1. So, if we were to add a row to the triangle above, the first entry would be 1, the next entry is obtained by adding the 1 and 5 in the previous row to get 6, the one after that is obtained by adding the 5 and 10 to get 15, etc. The next row would therefore be:
The entries in the triangle can therefore be defined quite elegantly using the following recursive function: 1, if r = 0 or if r = n C( n, r ) = C( n - 1, r - 1 ) + C( n - 1, r ), if 0 < r < n where C( n, r ) is the entry on the nth row in the rth position (where the first row is row 0 and the first position is position 0 ). So, for example, the 6 on row 4 can be generated by evaluating C( 4, 2 ) because the 6 sits on row 4 in position number 2. Similarly, the second 10 on row 5 can be generated by evaluating C( 5, 3 ) because the second 10 sits on row 5 in position number 3. Part 3a
Part 3b
|
||
| ©University of British Columbia | Last updated:
November 11, 2010 03:25 PM | |