CS 313 - Homework 2 - Pascal

Due: Wednesday 2/25 in class

This homework consists of two programming problems in Pascal. We will use the Pascal compiler "fpc" (FreePascal). For your work in this course, you should create a new directory "cs313" and make it readable only by you:
      cd
      mkdir cs313
      chmod go-rwx cs313
Within this directory, create a directory "hw2" and go to it:
      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 on benjerry in ~schar/cs313/pascal/. 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").

When you are done, hand in printouts of your code. On the first page, write your own name as well as the names of the students with whom you collaborated. Don't forget to answer question 4! Also submit your programs electronically by typing "submit313" in the directory containing your Pascal files.

  1. Read Sethi Chapters 3, 4, and 15.1 (on Pascal).

  2. Sethi p. 145, problem 4.9 (implement a binary search tree in Pascal). Implement the functions "member", "insert", and "print" as instructed. You will need to define a record type "node" that holds an integer and pointers to its 2 children nodes.

  3. Implement Conway's Game Of Life in Pascal. Try to match the behavior of the sample executable program "life" on benjerry in ~schar/cs313/hw2/ as closely as possible. A short description of the game follows; for (much) more information see the web, for example Paul Callahan's Life Page. 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.

    Use the following procedures to clear the screen or reposition the cursor (which make use of vt100 control sequences).

    procedure clear;
    { move cursor to top left corner and clear screen }
    begin
       write(chr(27)); { ascii code for escape }
       write('[2J') { control sequence to clear screen }
    end;
    
    procedure home;
    { move cursor to top left corner of screen }
    begin
       write(chr(27)); { ascii code for escape }
       write('[f'); { control sequence to move cursor home }
    end;
    
    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;
    
  4. How much time did it take you to complete this assignment?