* Sample Code for GHP #1 Taken from Nyhoff Page 42, Problem #2 Search the entries of the n x n matrix mat in rowwise order for an entry equl to the iterm Note: (1) code is incomplete….it can not be used as shown (2) even if complete it produces the wrong answer You must write a program that (1) creates and fills a matrix with positive integers (2) queries the user for a search value (3) reports whether or not the search value is in the matrix (4) the size of the matrix must be determined at compile-time */ bool found: int row, col: for (row = 0: row
Expert Answer
Your code is as follows:
// i will use i instead of row and j instead of col just for simplification
#include <iostream>
using namespace std;
int main()
{
int n; // for giving input to create n x n matrix
cout << “Enter the value for creating n x n matrix:”;
cin >> n;
int matrix[n][n];
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
cout << “A[” << i+1 << “][” << j+1 << “]=”;
cin >> matrix[i][j];
}
}
cout << “The matrix you entered is” << endl;
cout << “[ “;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
cout << matrix[i][j] << ” “;
}
if (i!=n-1)
cout << “]n [ “;
else
cout << “]”;
}
int value; // The Value you want to search
int flag = 0;
cout<< endl << ” Enter the Value you want to search :” ;
cin >> value;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
if( matrix[i][j] == value)
{
flag =1;
cout << “nValue found at location” << i+1 << “,” << j+1;
}
}
}
if ( flag == 0)
{
cout << ” The value You Entered Can’t found Please Try Again!”;
}
}
SCREENSHOTS:
OUTPUT SCREENSHOT:
i