forked from Knackie/algorithmshacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search_In_a_2D_Matrix.cpp
43 lines (36 loc) · 1.02 KB
/
Search_In_a_2D_Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// C++ program to search an element in row-wise
// and column-wise sorted matrix
#include <bits/stdc++.h>
using namespace std;
/* Searches the element x in mat[][]. If the
element is found, then prints its position
and returns true, otherwise prints "not found"
and returns false */
int search(int mat[4][4], int n, int x)
{
if (n == 0)
return -1;
// traverse through the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
// if the element is found
if (mat[i][j] == x) {
cout << "Element found at (" << i << ", "
<< j << ")\n";
return 1;
}
}
cout << "n Element not found";
return 0;
}
// Driver code
int main()
{
int mat[4][4] = { { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
// Function call
search(mat, 4, 29);
return 0;
}