Program to find maximum element in matrix

Maximum Element in a Matrix

In this post we will be discuss the Program to find the Maximum element in the matrix i.e., to find the number that is maximum or the largest in a given matrix either select it from the row or the column. Also, the required logic is discussed and the source code is written in C++ which is compiled successfully to provide the best results.

Maxium Element in the Matrix

Problem Description

Raju is the maths teacher in high secondary school and provided mark sheet to students. In this class room, students are arranged in the form of rows and columns. Raju needs to find the highest mark in this class. Help him to find out.

INPUT FORMAT:

The input consists of (m*n+2) integers.

The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of columns in the matrix.

The remaining integers correspond to the elements in the matrix.

The elements are read in row-wise order, the first row first, then the second row and so on.

Assume that the maximum value of m and n is 10.

OUTPUT FORMAT:

Refer to the sample output for details.

SAMPLE INPUT & OUTPUT:

3

2

4 5

6 9

0 3

The maximum element is 9

Explaination :

In this we will be discussing the Program to find the Maximum element in the matrix i.e., to find the number that is maximum or the largest in a given matrix either select it from the row or the column. This will be achieved using the nested loop in which an element with an initial value set will be compared with each of the value in the matrix, and hence if the value is find the greatest in the matrix it will be printed as the maximum value in the result part.

🔍Logic to follow to come-up with the solution :

1. Declare a function with the required arguments to use in the code.

2. Set a variable with maxElement which will hold the maximum value of the matrix after finding it.

3. Initialize the nested loop using the rows and columns values.

4. Now check for each value in the matrix and if found greater value then the previously defined then set it as maximum value to the maxElement variable.

       i.e.  if (mat[i][j] > maxElement) then , maxElement = mat[i][j];

5. Before closing the function return the maximum value found.

6. Now in the main function declare the matrix size and input all the values of the matrix.

7. At last call the declared function and print the final resulted value as asked in the problem.

Let’s write the required code👨‍💻 for the problem :

Code :

 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
#include<iostream>
#include <bits/stdc++.h> 
using namespace std;  
int findMax(int mat[10][10],int rows,int cols) 
{ 
      int maxElement = INT_MIN; 
  
    for (int i = 0; i < rows; i++) 
       { 
        for (int j = 0; j < cols; j++)
         { 
            if (mat[i][j] > maxElement)
            { 
                maxElement = mat[i][j]; 
            } 
         } 
       } 
      return maxElement; 
} 
  int main() 
{ 
  
   int N,M;
   cin>>N>>M;
  int mat[10][10];
  for(int i=0;i<N;i++)
    { 
      for (int j=0;j<M;j++)
     { 
      cin>>mat[i][j];
     }
    } 
   cout <<"The maximum element is "<<findMax(mat,N,M) << endl; 
   
    return 0; 
} 

Sample Test Cases have been passed successfully

Program to find Max in Matrix test case passed

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Next Post Previous Post
No Comment
Add Comment
comment url

Best Buy Products