// FILE: neighbors.cxx // AUTHOR: Beth Katz with David Hutchens // MODIFICATION HISTORY: created January 2001, updated January 2002 // PURPOSE: Reports on clumps of adjacent neighbors in an area. // Demonstrates program style that should be used for CS 162 in Spring 2002. // Assertions and pre- and post-conditions not required for first program. // INPUT: numRows and numColumns of area (integers between 1 and 20 inclusive) // pairs of integers giving row and column location of an entity where // row and column are each between 1 and 20 inclusive // OUTPUT: for each entity, how many neighbors it has #include // Provides assert #include // Provides cout and cin #include // Provides exit using namespace std; // Allows all Standard Library items to be used // Global constants const int MaxSize = 20; const int MaxSizePlus2 = MaxSize+2; // Prototypes for functions void fillNeighborhood(bool area[MaxSizePlus2][MaxSizePlus2], int rows, int cols); // Fill a two-dimensional array with whether or not an entity lives at // various locations. That information is obtained from the user. // Precondition: 1 <= rows <= MaxSize && 1 <= cols <= MaxSize // Postcondition: The valid locations provided by the user as row column // locations of entities will have been marked as true in the array. // A valid location is one where both row and column are between 1 and // rows and cols respectively. // All other locations will be false. // There will be a border of false values surrounding the valid values. void clearArea(bool area[MaxSizePlus2][MaxSizePlus2]); // Fill array with false values. // Precondition: // Postcondition: all elements of array are false int neighborCount(bool area[MaxSizePlus2][MaxSizePlus2], int row, int col); // Return a count of how many neighbors entity at row,col has. // Precondition: 1 <= row <= MaxSize && 1 <= col <= MaxSize // Postcondition: The integer returned is the count of how many of the // eight cells adjacent to row,col cell are true void reportNeighbors(bool area[MaxSizePlus2][MaxSizePlus2], int rows, int cols); // Report on how many neighbors each entity has. // Precondition: 1 <= rows <= MaxSize && 1 <= cols <= MaxSize // Postcondition: For each true cell in the range 1 to rows and 1 to cols, // prints the location of that cell and how many of its eight adjacent // cells are true int main( ) { int numRows, numCols; bool entities[MaxSizePlus2][MaxSizePlus2]; cout << "How many rows? (1-20) "; cin >> numRows; cout << "How many columns? (1-20) "; cin >> numCols; if (numRows < 1 || numRows > MaxSize || numCols < 1 || numCols > MaxSize) { cout << "Number of rows and columns must be between 1 and " << MaxSize << " not " << numRows << " and " << numCols << endl; exit(4); } fillNeighborhood(entities, numRows, numCols); reportNeighbors(entities, numRows, numCols); return 0; } void fillNeighborhood(bool area[MaxSizePlus2][MaxSizePlus2], int rows, int cols) { int r, c; int count = 0; assert(1 <= rows && rows <= MaxSize && 1 <= cols && cols <= MaxSize); clearArea(area); cout << endl << "Enter locations of entities as row and column pairs" << endl; cout << "where row is between 1 and " << rows; cout << " and column is between 1 and " << cols << endl; cout << "End input with control-d "<< endl; while (cout << "Next location? ", cin >> r >> c) { if (r < 1 || r > rows || c < 1 || c > cols) { cerr << r << "," << c << " is invalid location. Location ignored." << endl; } else { if (area[r][c]) { cerr << r << "," << c << " already occupied. Input ignored." << endl; } else { count++; area[r][c] = true; cout << "Entity " << count << " is at " << r << "," << c << endl; } } } cout << count << " entities placed." << endl; } void clearArea(bool area[MaxSizePlus2][MaxSizePlus2]) { int r, c; for (r=0; r < MaxSizePlus2; r++) { for (c=0; c < MaxSizePlus2; c++) { area[r][c] = false; } } } int neighborCount(bool area[MaxSizePlus2][MaxSizePlus2], int row, int col) { int count = 0; int dR, dC; // dR (delta row) and dC (delta column) determine a direction. assert(1 <= row && row <= MaxSize && 1 <= col && col <= MaxSize); // As each value moves between -1 and 1, it is added to the row and col // indexes to compute the indexes of the adjacent cell in that direction for (dR = -1; dR <= 1; dR++) { for (dC = -1; dC <= 1; dC ++) { if (dR || dC) { // 0,0 isn't a direction if (area[row+dR][col+dC]) { count++; } } } } return count; } void reportNeighbors(bool area[MaxSizePlus2][MaxSizePlus2], int rows, int cols) { int r, c; assert(1 <= rows && rows <= MaxSize && 1 <= cols && cols <= MaxSize); for (r=1; r <= rows; r++) { for (c=1; c <= cols; c++) { if(area[r][c]) { cout << "Entity at " << r << "," << c << " has " << neighborCount(area, r, c) << " neighbors" << endl; } } } }