CS 313 - Homework 2 - Pascal
Due: Wednesday 9/29 at 10amThis homework consists of two programming problems in Pascal. We will use the Pascal compiler "fpc" (FreePascal), for which you can find installation instructions on the course home page. You will also need an editor to edit your source files; I recommend using Atom with the "language-pascal" package (see video 5a, time 13:00).
By default this homework should be done by each student individually. However, if you get stuck and can't figure it out on your own, you have the option of doing this homework with another student for a grade penalty of 10%. In that case, you'll need to do both problems as a pair and submit only one copy of this homework. Each program needs to have both of your names at the top in a comment.
All Pascal files should end in ".p". To get started, look at the example Pascal programs linked from the course page. To compile a Pascal program, say, "hello.p", simply use the command "fpc hello.p". This will result in an executable "hello" which you can run by typing "./hello". (The compiler will also create an object file "hello.o" which you can ignore.)
- (Sethi problem 4.9) Implement a binary search tree (BST) of
integers in Pascal. Name your program bst.p. Recall that a
BST is either empty, or it consists of a node containing a value with
two BSTs as subtrees. Recall further that in a BST all values in the
left subtree are smaller than the node's value, and all values in the
right subtree are larger.
Implement the following:
- A data structure to represent the BST. You will need to define a record type "node" that holds an integer and pointers to its left and right subtrees.
- A function "member" to determine whether a value is stored in the tree.
- A procedure "insert" to add a new value to the tree. Do not store duplicate values in the tree -- if a duplicate value is added, the tree should not change (i.e., it implements a set data structure). In your insert procedure, pass the tree as a reference parameter ("var") since the procedure modifies the tree.
- A procedure "inorder" that does an in-order traversal of the tree and prints all the values in the tree (given that it's a BST, they should get printed in order).
- A main program that thoroughly tests your implementation and produces useful output demonstrating that your code works.
Hint: Don't use "dispose" anywhere -- we never need to de-allocate a node since we're not implementing a "remove" function.
- Implement Conway's Game Of Life in Pascal.
Name your program life.p.
A short description of the game follows; for (much) more information see the web, for example Wikipedia (and if you get hooked on Life, get the program Golly!). Briefly, the Game Of Life consists of a 2D grid of cells, who are either alive or dead. A new generation of cells is created from the current generation according to the following rules: For each cell, consider its 8 neighbors. If the current cell is alive, it will survive if it has 2 or 3 alive neighbors. If the current cell is dead, it will gain new life if exactly 3 of its 8 neighbors are alive. For example, the following pattern ('.'=dead, '#'=alive)
will change into
. . . . . . . # . . . . # . . . . # . . . . . . .
because the top and bottom cells die (1 neighbor), the middle cell survives (2 neighbors), and the two cells left and right of the middle are born (3 neighbors).
. . . . . . . . . . . # # # . . . . . . . . . . .
The borders of the grid deserve special attention. There are two approaches: either, pretend border cells don't have neighbors beyond the border; or, pretend the grid "wraps around" both from the bottom to the top and from the left to the right, so every cell in the grid has exactly 8 neighbors. For full credit, you should implement the second option, which is also the way my sample program behaves (see below). Hint: the "mod" function might come in handy here - but avoid taking the "mod" of negative numbers. By the way, a grid that "wraps around" in this way is topologically equivalent the surface of a torus (i.e., an object shaped like a doughnut).
Your initial population should be random, with a probability of 1/2 for each cell to be alive. You can use the function random(2), which will return either 0 or 1. To get different random numbers every time you run the program, call randomize at the beginning of your main program.
When you first run your program, it should print the first generation, and then ask the user how many iterations it should compute and print (with 0 to end). Use a grid size that fits on your screen e.g., 38 x 38. To get a rudimentary animation, have the program wait for 0.2 seconds after each generation is printed. Do do this, import the "sysutils" package by adding the statement "uses sysutils;" at the top of your program (after the "program" line). Then you can use the function sleep(200); to wait for 200ms after each generation is printed. (Note: the sleep command does not seem to work on Macs...)
Here are sample executables for Mac, Windows, and Linux: life-mac, life-windows.exe, and life-linux. Download the appropriate one onto your computer, change its permission to be executable (e.g. "chmod a+x life-mac"), then run it from the command line. (On a Mac, it might not let you run it since it was downloaded from the Internet. To override this, navigate to the file in the Finder, right-click, and select "Open".) Try to match the behavior of the sample with your program.
Hint: Make sure you pass all arrays by reference, especially for procedures that modify the array.
Online submissionUpload your files bst.p and life.p via the HW 2 submission page by 10am on Wed.