CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 2
    Due at the beginning of class, Wednesday, 2/5

    In today's lab you will get some practice debugging a program and working with objects in C++. In addition, we will work with some data hiding. In lecture you were introduced to the Life Class to play the game of life. The lectures and the book together presented the implementation of the life class.

    The code that was presented in class and in the book is available in the directory:

      ~csci132/labs/lab2

    Start the lab as follows:

    • Copy the code for lab1 into your labs folder by typing:
        cp -r ~csci132/labs/lab2 labs/.
    • Change directories into your new lab2 directory by typing:
        cd labs/lab2
    • Type ls to verify that the lab2 directory contains the following files:
      • life.h
      • life.cc
      • playLife.cc
    • Start up an emacs window.

    1. Debugging

    There are two kinds of errors in program code:
    • Syntax errors will generate compiler errors.
    • Runtime errors will not affect the compilation, but will cause errors when the program is executed.

    There are several errors of each type in the code you have copied over to your directory. Fix each of the errors (but keep track of each one as you fix it). To compile the program use the compile command:

      g++ -g -Wall playLife.cc life.cc -o playLife

    When the program is running correctly, print out a copy of each of the three files and circle the places where there were errors. Write a short comment on the printout indicating what the error was, and whether it generated a syntax error or a runtime error. On a separate sheet, list all the errors you found. To test for the runtime errors, test your code on the following three in a row input pattern:

      xxx

    Which is generated with the following coordinates:

      10 10
      10 11
      10 12
      -1 -1


    2. A new way to implement the Life class.

    In lecture we learned that it is important to be careful about how a program handles extreme cases. In the Life class, we thought carefully about how to treat cells on the edge of the board. The solution we developed in class was to put a "hedge" around the board. In other words, the board was surrounded by a rim of cells that always remained with value 0, but could be counted as neighbors of the edge cells.

    An alternative way to deal with the edges of the board is to "wrap around" when counting neighbors. The term "wrap around" means that the left side will be connected to (neighboring) the right side and the top will be connected to the bottom. Thus any cell with position (1, col), will have a neighbor at (maxRow, col). Any cell with position (row, 1) will have a neighbor at (row, maxCol).

    So a cell at position (3, 1), will have the following neighbors:

      (2, maxCol), (2, 1), (2, 2)
      (3, maxCol), (3, 2)
      (4, maxCol), (4, 1), (4, 2)

    Your task is to rewrite life.h and life.cc so that the board uses the wrap-around method to deal with edges. Be careful about the corners! Specifically you will need to change the following:

    • Save your life.cc file as life1.cc and life.h and life1.h
    • Change the #include "life.h" statement in life1.cc and playLife.cc to be #include "life1.h"
    • Change the size of the grid in life1.h to have only maxrow rows and maxcol columns.
    • Change initialize( ) in life1.cc to take into account the new grid coordinates.
      Note: Since the change should be transparent to the user, when the user enters (1, 1), the position (0, 0) in the grid array should be made a 1.
    • Change neighbor_count( ) in life1.cc so that it wraps around.
      Note: You will have to test if the index of the array is off the grid.
      If it is, you must change it to wrap to the other side.
      The index, -1, changes to maxrow -1 (or maxcol -1).
      The index, maxrow (or maxcol), changes to 0 (zero).
      Make sure you do not change the counters in your for loops!
    • Change update( ) in life1.cc to take into account the new grid size and grid coordinates.
    • Change print( ) in life1.cc to take into account the new gridsize and coordinates.
    • Compile your code with the following command:
      g++ -g -Wall playLife.cc life1.cc -o playLife
      Note the life1.cc here.

    Test your program on the following:

      Configuration 1:
      1 10
      1 11
      1 12
      -1 -1

      Configuration 2:
      1 60
      2 60
      3 60
      -1 -1

    Can you think of other configurations that should be tested? Do so!

    What To Turn In.

    • A printout of the three debugged files: life.h, life.cc and playLife.cc, with the errors circled and noted.
    • A written list of the errors you found and corrected.
    • A printed listing of each of the two files, life1.cc and life1.h.
    • Submit the softcopy of your lab by typing:

      ~csci132/bin/submit csci132 lab2

    Be sure that the program prologue for each file contains your name, course, lecture section, date and purpose of the program or a description of the contents of the file (e.g. specification of the Life class).

    Reminder.
    Be sure to save a copy of each file in your labs directory. It is your responsibility to keep the copies of all programs written for this course until your graded assignment is returned.


    Home | | Syllabus | | Assignments | | Lecture Notes