#include using namespace std; void initialize (char [][5], int, int); void display(char [][5], int,int); int main() { char Board[5][5]; int row, col, i, j; initialize(Board,5,5); display(Board,5,5); cout << " enter row, col coords of element to examine" << endl; cin >> row >> col; while(row < 0 || row >=5 || col < 0 || col >=5) { cout << "bad entry, enter again" << endl; cin >> row >> col; } cout << " immediate neighbors " << endl; for (i = row-1; i <= row+1; i++) // check all 8 neighbors for (j = col-1; j <= col+1; j++) { if((i >= 0) && (i < 5) && (j >= 0) && (j < 5)) // we're in the matrix { if (i != row || j != col) if (Board[i][j] == 'x') cout << " Board[" << i << "][" << j <<"] has an x" << endl; else cout << " Board[" << i << "][" << j <<"] has no x" << endl; } } //check vertical and horizontal neighbors cout << endl << " vertical neighbor above " << endl; i = row -1; //vertically above j = col ; if ((i >= 0) && (i < 5) && (j >= 0) && (j < 5)) if (Board[i][j] == 'x') cout << " Board[" << i << "][" << j <<"] has an x" << endl; else cout << " Board[" << i << "][" << j <<"] has no x" << endl; cout << endl << " vertical neighbor below " << endl; i = row +1; //vertically below j = col ; if ((i >= 0) && (i < 5) && (j >= 0) && (j < 5)) if (Board[i][j] == 'x') cout << " Board[" << i << "][" << j <<"] has an x" << endl; else cout << " Board[" << i << "][" << j <<"] has no x" << endl; return 0; } void initialize (char Board[][5], int rows, int cols) { int i,j; for (i=0; i< rows; i++) // set each element of array to '-' for (j=0; j < cols; j++) Board[i][j] = '-'; //place some random letters on the board Board[1][3] = 'x'; Board [1][2] = 'w'; Board [3][4] = 'q'; Board [4][4] = 'w'; return; } void display (char Board[][5], int rows, int cols) { int i,j; cout << ' '; // to line up my col labels for (i=0; i < cols; i++)// display col values for readability cout << i; for (i=0; i< rows; i++) { cout << endl << i; for (j=0; j < cols; j++) cout << Board[i][j]; } cout << endl; return; }