C Write a program to search out array matrix for a particula
Solution
//this code has been tested on gcc compiler
#include <iostream>
using namespace std;
int main()
{int r,c,key; //variable for storing no.of rows,coloumns and key to be searched
int flag=0;//flag variable
cout << \"Enter the no.of rows of matrix\" << endl; //asking the user to enter number of rows in the matrix
cin>>r; //storing rows entered in r
cout << \"Enter the no.of coloumns of matrix\" << endl; //asking the user to enter number of coloumns in the //matrix
cin>>c; //storing coloumns entered in c
int matrix[r][c]; //defining matrix
cout << \"Enter the elements of the matrix\" << endl; //asking user to enter elements of the matrix
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
cin>>matrix[i][j]; //storing user entered value in matrix
}
cout << \"Enter the key you want to search \" << endl; //asking the user to enter the key for the search
cin>>key; //storing user entered key in variable key
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
if(matrix[i][j]==key) //if key is available in matrix,set the flag=1
flag=1;
}
if(flag) //if flag is set i.e 1,print key is found
cout << \"Found \" << endl;
else //else key is not found
cout << \"Not found \" << endl;
return 0;
}
//end of code
******OUTPUT*****
Enter the no.of rows of matrix
2
Enter the no.of coloumns of matrix
2
Enter the elements of the matrix
1
2
3
4
Enter the key you want to search
3
Found
******OUTPUT*****
Note:please ask in case of any doubt,Thanks.
