Be sure to Reload these files periodically, because they may not be in its final form.

Save paper. Do not blindly print files. There are a number of files included with this assignment that you should not print.

To print this page, print the following version: This Page (2up Postscript)

COSC 1030 Introduction to Computer Science II
Assignment 3: Recursion
Due: Wednesday, March 19, 1997, 10:30 am (SHARP)

1.1) Learning Objectives

1.2) What is given

1.3) What to hand in 


2) Learning about Recursive Figures


3) Drawing Recursive Figures


4) Finding the Connection Between Recursive Pictures and Recursive Programs.


5) Study the recursive code


6) Write code for the programs Copy, Eval and Put.


7) Formal Proof of Correctness

The following is an outline of a proof of correctness for a recursive program. Fill in the missing details required to prove the correctness of your program EvalRec.

For every integer k >=0, let S(k) be the formal statement that "The routine EvalRec returns the correct value for every value of x, y, and z and for every polynomial (pointed to by n) that has k nodes in it."

The overall goal is to prove that EvalRec returns the correct answer on every input. It is sufficient to prove that "forall k>=0 S(k)".

The method is proof by Strong Induction:
       i.e. prove that for every k>=0, [S(0) and ... and S(k-1)] => S(k)
       Note for k=0, the assumption is empty and you must prove S(0).

Let k be an arbitrary integer >= 0. Assume by way of strong induction that [S(0) and ... and S(k-1)].

Because we assumed [S(0) and ... and S(k-1)] and then proved S(k), it follows that [S(0) and ... and S(k-1)] => S(k). Because we did this for an arbitrary value of k>=0, it must be true for all values of k>=0.

By the method of Strong Induction, we can conclude that "forall k>=0 S(k)", i.e. EvalRec works.


8) Tracing the execution of a recursive program

Study the code provided for the routine PrettyPrint. Trace the execution of this program with the following polynomial as input:

          |-- 1
      --*-|
          |       |-- 2
          |   |-*-|
          |   |   |   |-- 3
          |   |   |-+-|
          |   |       |-- 4
          |-+-|
              |-- 5

Each time a routine (PrettyPrint or PPrintRec) is called, draw a box. This is referred to as an "instance" of the routine. (The block of memory storing the local variables for this instance is called a "stack frame".) Connect the instances with arrows from the calling to the called instance. Because each instance potentially calls two other instances, the trace diagram will look like a tree. Include with each such instance:


9) Running the Demo Program

Create your own example demonstration input file in "mysample.in". It should be approximately the same length as sample.in.

Copy and execute the shell script program rundemo. Make sure you have set its protection mode to executable. The script runs the demonstration program polyTree.dem on sample.in and outputs a comparision of your output with sample.out. It then runs polyTree.dem on your test file "mysample.in". The output is put into the file "rundemo.out".


10) Assertion Testing

Write a short, no more than one page, no input demonstration program, polyTreeAssert.dem. As done in assignment 1, if no bugs are found, then it should only output a single statement "Example assertions are true."