CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 2 Solutions

    Solution to life1.cc code:

    /*******************************************************************
    * Name: Brenda Student
    * Date: 1/28/14
    * Course: CSCI 132
    * Assignment: Lab2
    * Instructor: Royden
    * Program: life1.cc
    * Purpose: Implementation of Life class with wrap around grid.
    ********************************************************************/
    #include "life1.h"
    
    int Life::neighbor_count(int row, int col)
      /*
    Pre:  The Life object contains a configuration, and the coordinates
          row and col define a cell inside its hedge.
    Post: The number of living neighbors of the specified cell is returned.
      */
    
    {
      int i, j;
      int nrow, ncol;
      int count = 0;
      for (i = row - 1; i <= row + 1; i++)
        for (j = col - 1; j <= col + 1; j++) {
          nrow = i;
          ncol = j;
          if (i < 0) {
            nrow = maxrow - 1;
          }
          if (j < 0) {
            ncol = maxcol-1;
          }
          if ( i >= maxrow ) {
            nrow = 0;
          }
          if (j >= maxcol) {
            ncol = 0;
          }
      
          count += grid[nrow][ncol];  //  Increase the count if neighbor is alive.
        }
      count -= grid[row][col]; //  Reduce count, since cell is not its own neighbor
      return count;
    }
    
    
    void Life::update()
      /*
    Pre:  The Life object contains a configuration.
    Post: The Life object contains the next generation of configuration.
      */
    
    {
      int row, col;
      int new_grid[maxrow][maxcol];
    
      for (row = 0; row < maxrow; row++)
        for (col = 0; col < maxcol; col++)
          switch (neighbor_count(row, col)) {
          case 2:
            new_grid[row][col] = grid[row][col];  //  Status stays the same.
            break;
          case 3:
            new_grid[row][col] = 1;                //  Cell is now alive.
            break;
          default:
            new_grid[row][col] = 0;                //  Cell is now dead.
          }
    
    
      for (row = 0; row < maxrow; row++)
        for (col = 0; col < maxcol; col++)
          grid[row][col] = new_grid[row][col];
    }
    
    
    
    
    void Life::initialize()
      /*
    Pre:  None.
    Post: The Life object contains a configuration specified by the user.
      */
    
    {
      int row, col;
      for (row = 0; row < maxrow; row++)
        for (col = 0; col < maxcol; col++)
          grid[row][col] = 0;
      cout << "List the coordinates for living cells." << endl;
      cout << "Terminate the list with the special pair -1 -1" << endl;
      cin >> row >> col;
    
      while (row != -1 || col != -1) {
        if (row >= 1 && row <= maxrow)
          if (col >= 1 && col <= maxcol)
            grid[row-1][col-1] = 1;
             else
               cout << "Column " << col << " is out of range." << endl;
          else
            cout << "Row " << row << " is out of range." << endl;
        cin >> row >> col;
      }
    }
    
    void Life::print()
      /*
    Pre:  The Life object contains a configuration.
    Post: The configuration is written for the user.
      */
    
    {
      int row, col;
      cout << "\nThe current Life configuration is:" << endl;
      for (row = 0; row < maxrow; row++) {
        for (col = 0; col < maxcol; col++)
          if (grid[row][col] == 1) cout << '*';
          else cout << ' ';
        cout << endl;
      }
      cout << endl;
    }
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes