Find the position of an element in a 2d array

Problem Statement:- Program to find the position of an element in a 2d array or Matrix.

Sample Input/Output:-


Sample Input First:2 2

6 7 8 9

9

Sample Output First: 

(1, 1)

Sample Input Second: 3 3

6 7 8 
9 3 2
1 2 4
5

Sample Output Second: 

Given Search Element is not found....


Data requirement:-


  Input Data:- row_size, col_size, matrix[][]


  Output Data:-Search_ele,row,col


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to find the position of an element in a 2d array or Matrix.


#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } //Take input of search Element int Search_ele; printf("Enter the search element:"); scanf("%d",&Search_ele); //Compute the position of the search Element int flag=0, row, col; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]==Search_ele) { flag=1; row=i; col=j; } } } //Display position of the search Element if(flag==1) printf("Position of Search Element %d is (%d, %d)",Search_ele,row,col); else printf("Given Search Element is not found...."); }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 6 7 8 9 Enter the search element:9 Position of Search Element 9 is (1, 1)


Program in C++

Here is the source code of the C++ Program to find the position of an element in a 2d array or Matrix.


#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } //Take input of search Element int Search_ele; printf("Enter the search element:"); scanf("%d",&Search_ele); //Compute the position of the search Element int flag=0, row, col; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]==Search_ele) { flag=1; row=i; col=j; } } } //Display position of the search Element if(flag==1) printf("Position of Search Element %d is (%d, %d)",Search_ele,row,col); else printf("Given Search Element is not found...."); }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 7 2 3 1 2 4 5 6 5 Enter the search element:6 Position of Search Element 6 is (2, 1)


Program in Java

Here is the source code of the Java Program to find the position of an element in a 2d array or Matrix.


import java.util.Scanner; public class FindPositionOfElement { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } //Take input of search Element int Search_ele; System.out.print("Enter the search element:"); Search_ele=cs.nextInt(); //Compute the position of the search Element int flag=0, row=0, col = 0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]==Search_ele) { flag=1; row=i; col=j; } } } //Display position of the search Element if(flag==1) System.out.println("Position of Search Element "+Search_ele+" is ("+row+","+col+")"); else System.out.println("Given Search Element is not found...."); cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
6 7 8 
9 3 2
1 2 4
Enter the search element:5
Given Search Element is not found....


Program in Python

Here is the source code of the Python Program to find the position of an element in a 2d array or Matrix.


# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) #Take input of search Element Search_ele=int(input("Enter the search element:")) #Compute the position of the search Element flag=0 row=0 col=0 for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]==Search_ele: flag = 1 row = i col = j # Display position of the search Element if flag==1: print("Position of Search Element ",Search_ele," is (",row,",",col,")") else: print("Given Search Element is not found....")


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 5 6 7 8 9 2 3 4 Enter the search element:9 Position of Search Element 9 is ( 1 , 2 )



Most Recommend Questions:-








More Questions:-





Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments