0% found this document useful (0 votes)
23 views2 pages

KARTIKEY

This C++ program takes a 2D matrix as input from the user along with its size and an element to search for. It uses nested for loops to iterate through the matrix and compare each element to the one being searched for. If a match is found, it prints "Element found", otherwise it prints "Element not found".

Uploaded by

Kartikeya Tandon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

KARTIKEY

This C++ program takes a 2D matrix as input from the user along with its size and an element to search for. It uses nested for loops to iterate through the matrix and compare each element to the one being searched for. If a match is found, it prints "Element found", otherwise it prints "Element not found".

Uploaded by

Kartikeya Tandon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Program for searching an element in the MATRIX

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{ clrscr();
void search(int[50][50],int,int,int);
int A[50][50],i,j,m,n,k;
cout<<"Enter the size of matrix";
cin>>m>>n;
cout<<"\n Enter elements in matrix";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++) //loop for reading matrix
cin>>A[i][j];
}
cout<<"\n Enter element to be searched";
cin>>k;
search(A,m,n,k); //reading the element
getch();
}
void search(int A[50][50],int m,int n,int k)
{ int i,j;
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ if(k==A[i][j])
{ cout<<"\n Element found";
getch();
exit(0);
}
}
}
cout<<"\n Element not found";
}

OUTPUT
Enter the size of matrix 2
2
Enter elements in matrix 1
2
3
4
Enter element to be searched
7
Element not found

You might also like