CS 313 - Homework 2 - Pascal

Due: Wednesday 3/1 at 11:15am

Update: If you get stuck and can't figure it out on your own, you have the option of doing this HW with another student for a grade penalty of 10%. In that case, you need to do both problems as a pair and submit only one copy of this HW. Each program needs to have both of your names at the top in a comment.

Common problems / tips:
Problem 1 (BST):

Problem 2 (Life): In case you're using a different version of Pascal, make sure your program compiles with fpc since that's what I'll use for testing.


This homework consists of two programming problems in Pascal. We will use the Pascal compiler "fpc" (FreePascal), which is installed on the lab machines. You are welcome to install it on your own computer too, if you want. See the CS 313 installation instructions by Ruben.

If you work on the lab machines, create a new directory "cs313", make it readable only by you, and create a directory "hw2" within it:

cd
mkdir cs313
chmod go-rwx cs313
cd cs313
mkdir hw2
cd hw2
You are now ready to create the files for this assignment. All Pascal files should end in ".p". To get started, look at the example Pascal programs in my directory ~schar/cs313/pascal/ which is also 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" (it will also create an object file "hello.o").

  1. Read Sethi Chapters 3, 4, and 15.1 (on Pascal). Also check out the Pascal tutorial linked from the main course page and look at the posted examples.

  2. Sethi p. 145, problem 4.9 (implement a binary search tree in Pascal). Name your program bst.p. Implement the functions "member", "insert", and "print" as instructed. 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). You will need to define a record type "node" that holds an integer and pointers to its 2 children nodes. In your main program, add code that tests all your functions, and produces useful output.

  3. Implement Conway's Game Of Life in Pascal. Name your program life.p. Try to match the behavior of the sample executable program "life" on the CS lab machines in ~schar/cs313/hw2/ as closely as possible. 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 three 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. 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.

    For a smooth animation you want to clear the screen initially, but then after each iteration simply reposition the cursor back to the top left corner without erasing the screen to minimize flicker. You can import a library for screen control by adding the statement "uses crt;" at the top of your program (after the "program" line). Then you can use the functions ClrScr; to clear the screen and GotoXY(1, 1); to move the cursor to the top left corner. If you happen to need to print a bunch of backspaces, use the following procedure:

    procedure backspace(n : integer);
    { move cursor to the left n spaces }
    var i : integer;
    begin
       for i:=1 to n do
          write(chr(8)); { ascii code for backspace }
    end;
    

Online submission

Upload your files bst.p and life.p on the HW 2 submission page by the beginning of class on Wed.