CS 210 Intermediate Computing with Data Structures
Programming Assignment 4
Spring 2006

Due Monday April 10, noon

Purpose

This assignment aims to help you:

·         Work with recursion

·         Work with files

·         Do more (simple) parsing

·         How to return 2 answers at once from a method: the Pair pattern.

·        See how we can generate  a Set of Strings recursively.

Reading

Before working on this assignment, you should read chapter 7 to pg. 266 in the text.

Description

This assignment is for you to do 1 and 2, and understand the solution of 3 (you can do it, of course.)  They all use recursion.

1.       Problem 7.32.  Let’s just use C programs, not C++, since they also have #include “filename”.   This form expects “filename” in the current directory,   (Don’t worry about #include <filename>, which looks in a search path for filename.)  Assume the #include and “filename” strings have no internal spaces, but there can be one or more spaces or other whitespace between these two strings on a line of the file, so use a Scanner with the default whitespace delimiter.  Call your program Expand.java.  Usage: “java Expand test.c ” should write the expanded text to test.i, the usual suffix for such expansions, overwriting any old files of the same name.  To System.out, write a report like the following, for the case of one header inclusion line ‘#include “header.h”’ in test.c, and where the header file header.h has two #include’s in it, one for header1.h and the other for header2.h.  Note how the indentation of the report mirrors the depth of the recursion, with 3 spaces of indentation per level.  If any include file is inaccessible, abort the run with an error message.

processing test.c
   processing header.h
      processing header1.h
      done with header1.h
      processing header2.h
      done with header2.h
   done with header.h
done with test.c

2.       Problem 7.27a.  Note that C(N,k) is the number of combinations of k things chosen from N, so we’ll call the class “Combo” for short. Combo should count calls to the recursive function as well as compute C(N, k), and provide the resulting call counts to the client code.  Class ComboTest has the driver. Usage “java ComboTest n k”, for example “java ComboTest 4 2” should yield “C(4, 2) = 6, using 5 calls”. C(4, 2) is the number of different 2-sets you can choose from 4 objects. The four unordered pairs (2-sets) from {a, b, c, d} are {a, b}, {a,c}, {a, d}, {b,c}, {b, d}, and {c, d}.   It is easy to see that this takes 5 calls, for C(4,2), C(3,2), C(3, 1), C(2,2), C(2,1) and C(2,1) again.

3.       (Optional to program, but understand algorithm and solution.) Now find the actual subsets that were counted by C(N, k).  We’ll use a set of Strings S(N, k) to represent all these subsets, for example S(4, 2) = {“ab”, “ac”, “ad”, “bc”, “bd”, “cd”} to represent all the 2-sets of a set of four.  We note that the set of such strings for N and k, S(N, k), can be obtained from the sets for N-1 as follows:  The set S(N-1, k),  such as S(3,2) here, gives us all the k-sets involving the first N -1 elements (here 2-sets involving a, b, and c:  S(3,2) = {“ab”, “ac”, “bc”}).  The rest of the needed strings for case N involve the last element, here d: for these we need to take S(N-1, k -1), here S(3,1) = {“a”, “b”, “c”}, and append the last element to each, here “d”, yielding “ad”, “bd”, and “cd”.  These 3 strings need to be added to the first set, bringing the total there to 6, so S(4, 2) = {“ab”, “ac”, “ad”, “bc”, “bd”, “cd”}.   Usage “java ComboSet n k”, which for n=4, k=2 should output S(4, 2) = {“ab”, “ac”, “ad”, “bc”, “bd”, “cd”}.

Design

These are simple programs in terms of classes, not requiring object-oriented design, since we are concentrating on recursion here, a control structure. In Problems 1 and 2, don’t create multiple objects of your own design (JDK supplied objects such as StringBuffers and Strings are fine, within reason) other than Pair objects. Problem 1 requires an additional parameter, level, for the recursive function and code to increment it appropriately, to guide the number of spaces to output to get the indentation right.  Be sure to keep your classes sealed up.  Arrange to deliver the C(N,k) value and number of calls together in a Pair object, discussed in the text on pg. 83.  Also see http://yan.codehaus.org/api/jfun/util/Pair.html for an example API, but you can choose the method names—full words are considered better style for names.

For 3., note that “char ch = (char)(‘a’ + 1);” produces a char ‘b’, etc., as we discussed when looking at the example about printing integers.  Assume that N <= 26, so the letters don’t run out.  Use a JDK set implementation, and note that creation of set objects during recursion is needed here.

memo.txt

In the file memo.txt:

1.      What problems did you encounter and how did you solve them? 

2.      Did you use the textbook examples to help you?  Which ones?  Other books you’d like to recommend?

3.      Did you access any information on the Web during this work?  Where?

4.      For Expand.java, give the method header for your recursive function and explain exactly what it promises to its caller, using the names of the arguments.  This explanation should also be in the header comment.

5.      Run Combo for C(4, 2), C(10, 5), C(16, 8), and enough other values to find a case that takes 10 million calls.  You should be aware that this is not the best way to calculate these numbers.   It is better to build up Pascal’s triangle until it’s big enough to cover the needed cases.

6.      Show that these call counts in Combo (and thus T(N) for C(N, N/2)) is numerically approximately exponential in N  Recall that exponentials have the property of increasing by a certain ratio when we add one to the “x” variable, here N.  What ratio do you find going from N=4 to N=5?  From N=10 to N=11? 

7.      In class Combo, you needed to provide the call count for the recursive function as well as the normal C(n, k) return value, two integer return values for the same computation, so you need a Pair object to return them together.  Did you look at the linked API? Did you change it?  Discuss your thinking and decision.

8.      For 3., trace the computation for S(4,2): Here’s a start, using indentation as usual for recursion level:

S(4, 2):
   calls S(3, 2):
     calls S(2, 2) returns {“ab”}
     calls S(2, 1) returns {“a”, “b”}
     adds “c” to each of second set, “ac”, “bc”, adds to first, yielding {“ab”, “ac”, “bc”} to return for S(3,2)

Turn In

Use the turnin system to submit your files.  Before the due date, submit the following files to the pa04 folder of your turnin system account:

·          memo.txt

·          Expand.java  Usage: java Expand <C-filename>

·          Combo.java

·          Pair.java

·          ComboTest.java  Usage: java ComboTest n k

·          ComboSet.java (optional)  Usage: java ComboSet n k

We will run a program that compiles and tests the Java code, and collect all files for the grader. It is your responsibility to make sure those files are present, that their names are spelled correctly (e.g. we will not find Memo.txt or memo.TXT) and that the files are turned in for the proper assignment folder.