/* matrix.cpp is a program to read a 3 X 3 matrix of integers mat and * an integer item, and search mat to see if it contains item. * * Input: 3 X 3 matrix mat and an item to search for in it * Output: A found/not found message *************************************************************************/ #include using namespace std; const int DIMENSION = 3; typedef int Matrix[DIMENSION][DIMENSION]; bool MatrixSearch(Matrix mat, int n, int item); int main() { // Enter the matrix Matrix mat; cout << "Enter the elements of the " << DIMENSION << " X " << DIMENSION << " matrix rowwise:\n"; for (int i = 0; i < DIMENSION; i++) for (int j = 0; j < DIMENSION; j++) cin >> mat[i][j]; // Search mat for various items int itemToFind; char response; do { cout << "Enter integer to search for: "; cin >> itemToFind; if (MatrixSearch(mat, DIMENSION, itemToFind)) cout << "item found\n"; else cout << "item not found\n"; cout << "\nMore items to search for (Y or N)? "; cin >> response; } while (response == 'Y' || response == 'y'); } /* MatrixSearch searches the entries of the n X n matrix * mat in rowwise order for an entry equal to item. * * Receive: Matrix mat, integers n and item * Return: true if item is found in mat, false otherwise ****************************************************************/ bool MatrixSearch(Matrix mat, int n, int item) { bool found; for (int row = 0; row < n; row++) for (int col = 0; col < n; col++) if (mat[row][col] == item) found = true; else found = false; return found; }