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.
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.
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.
Refer to the sample output for details.
SAMPLE INPUT & OUTPUT:
The maximum element is 9
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 :
Sample Test Cases have been passed successfully
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 !!