CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 10 Solutions

    Solution to neighbor_count function code:

    /*******************************************************************
     * Name: Brenda Student
     * Date: 4/21/14
     * Course: CSCI 132 Section 01
     * Assignment: Lab 10
     * Instructor: Royden
     * Function: neighbor_count()
     * Purpose: Count the neighbors of a given cell in the life class
     ***************************************************************************/
    
    
    int Life::neighbor_count(int x, int y)const
    /*Pre: None
    / Post: Returns the number of living neighbors of the cell at position x, y */
    {
      int count = 0;
      for (int x_add = -1; x_add < 2; x_add++)
        for (int y_add = -1; y_add < 2; y_add++)
          if (is_living->retrieve(x + x_add, y + y_add)) 
            count++;
      if (is_living->retrieve(x, y)) 
        count--;
      return count;
    }
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes