Find the minimum element in the matrix

Problem Statement:- Program to find the minimum element in the matrix.

Sample Input/Output:-


Sample Input First:2 2

6 7 2 3

Sample Output First: 

2

Sample Input Second: 3 3

44 42 32 56 65 14 21 12 19

Sample Output Second: 

12


Data requirement:-


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


  Output Data:-min


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to find the minimum element in the matrix.


Code:


#include<stdio.h> #include<limits.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]); } } //compute the minimum element of the given 2d array int min=INT_MAX; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]<=min) min=matrix[i][j]; } } //Display the smallest element of the given matrix printf("The Minimum element of the Given 2d array is: %d",min); }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 6 7 2 3 The Minimum element of the Given 2d array is: 2


Program in C++

Here is the source code of the C++ Program to find the maximum element in the matrix.


Code:


#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]; } } //compute the minimum element of the given 2d array int min=INT_MAX; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]<=min) min=matrix[i][j]; } } //Display the smallest element of the given matrix printf("The Minimum element of the Given 2d array is: %d",min); }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 44 42 32 56 65 14 21 12 19 The Minimum element of the Given 2d array is: 12


Program in Java

Here is the source code of the Java Program to find the minimum element in the matrix.


Code:


import java.util.Scanner; public class FindMinimumElement { 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(); } } //compute the minimum element of the given 2d array int min=Integer.MAX_VALUE; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]<=min) min=matrix[i][j]; } } //Display the smallest element of the given matrix System.out.println("The Minimum element of the Given 2d array is: "+min); cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
9 5 6
4 2 3
7 5 1
The Minimum element of the Given 2d array is: 1


Program in Python

Here is the source code of the Python Program to find the minimum element in the matrix.


Code:


import sys # 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()]) #compute the minimum element of the given 2d array min=sys.maxsize for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]<=min: min=matrix[i][j] # Display the smallest element of the given matrix print("The Minimum element of the Given 2d array is: ",min)


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 The Minimum element of the Given 2d array is: 6



Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments