Searching in a Matrix Data Structure
We can search an element in a matrix by traversing all the elements of the matrix.
Below is the implementation to search an element in a matrix:
#include <bits/stdc++.h>
using namespace std;
bool searchInMatrix(vector<vector<int> >& arr, int x)
{
int m = arr.size(), n = arr[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == x)
return true;
}
}
return false;
}
// Driver program to test above
int main()
{
int x = 8;
vector<vector<int> > arr
= { { 0, 6, 8, 9, 11 },
{ 20, 22, 28, 29, 31 },
{ 36, 38, 50, 61, 63 },
{ 64, 66, 100, 122, 128 } };
if (searchInMatrix(arr, x))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
// Java code for the above approach
import java.io.*;
class GFG {
static boolean searchInMatrix(int[][] arr, int x)
{
int m = arr.length, n = arr[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == x)
return true;
}
}
return false;
}
public static void main(String[] args)
{
int x = 8;
int[][] arr = { { 0, 6, 8, 9, 11 },
{ 20, 22, 28, 29, 31 },
{ 36, 38, 50, 61, 63 },
{ 64, 66, 100, 122, 128 } };
if (searchInMatrix(arr, x)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
// This code is contributed by lokeshmvs21.
# Python code for above approach
def searchInMatrix(arr, x):
# m=4,n=5
for i in range(0, 4):
for j in range(0, 5):
if(arr[i][j] == x):
return 1
return
x = 8
arr = [[0, 6, 8, 9, 11],
[20, 22, 28, 29, 31],
[36, 38, 50, 61, 63],
[64, 66, 100, 122, 128]]
if(searchInMatrix(arr, x)):
print("YES")
else:
print("NO")
# This code is contributed by ishankhandelwals.
// C# code for the above approach
using System;
public class GFG {
static bool searchInMatrix(int[,] arr, int x) {
int m = arr.GetLength(0), n = arr.GetLength(1);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i, j] == x)
return true;
}
}
return false;
}
public static void Main(string[] args) {
int x = 8;
int[,] arr = { { 0, 6, 8, 9, 11 },
{ 20, 22, 28, 29, 31 },
{ 36, 38, 50, 61, 63 },
{ 64, 66, 100, 122, 128 }
};
if (searchInMatrix(arr, x)) {
Console.WriteLine("YES");
} else {
Console.WriteLine("NO");
}
}
}
<script>
// JavaScript code for the above approach
function searchInMatrix(arr, x) {
let m = arr.length, n = arr[0].length;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (arr[i][j] == x)
return true;
}
}
return false;
}
// Driver program to test above
let x = 8;
let arr
= [[0, 6, 8, 9, 11],
[20, 22, 28, 29, 31],
[36, 38, 50, 61, 63],
[64, 66, 100, 122, 128]];
if (searchInMatrix(arr, x))
document.write("YES" + "<br>");
else
document.write("NO" + "<br>");
// This code is contributed by Potta Lokesh
</script>
Output
YES
Introduction to Matrix or Grid Data Structure – Two Dimensional Array
Matrix or Grid is a two-dimensional array mostly used in mathematical and scientific calculations. It is also considered as an array of arrays, where array at each index has the same size. In this article, we will cover all the basics of Matrix, the Operations on Matrix, its implementation, advantages, disadvantages which will help you solve all the problems based on Matrix Data Structure.
Table of Content
- What is a Matrix Data Structure?
- Representation of Matrix Data Structure
- Declaration of Matrix Data Structure
- Initializing Matrix Data Structure
- Operations on Matrix Data Structure
- Access elements of Matrix Data Structure
- Traversal of a Matrix Data Structure
- Searching in a Matrix Data Structure
- Sorting Matrix Data Structure
- Applications of Matrix Data Structure
- Advantages of Matrix Data Structure
- Disadvantages of Matrix Data Structure
- More Practice problems on Matrix Data Structure
Contact Us