
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find the maximum element in a Matrix in C++
In this problem, we are given a matrix of size nXm. Our task is to create a program to find the maximum element in a Matrix in C++.
Problem Description − Here, we need to simply find the largest element of matrix.
Let’s take an example to understand the problem,
Input
mat[3][3] = {{4, 1, 6}, {5, 2, 9}, {7, 3, 0}}
Output
9
Solution Approach
The solution to the problem is by simply traversing the matrix. This is done by using two nested loops, and checking whether each element of the matrix is greater than maxVal. And return the maxVal at the end.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; #define n 3 #define m 3 int CalcMaxVal(int mat[n][m]) { int maxVal = mat[0][0]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (mat[i][j] > maxVal) maxVal = mat[i][j]; return maxVal; } int main(){ int mat[n][m] = {{4, 1, 6},{5, 2, 9},{7, 3, 0}}; cout<<"The maximum element in a Matrix is "<<CalcMaxVal(mat); return 0; }
Output
The maximum element in a Matrix is 9
- Related Articles
- JavaScript Program to Find maximum element of each row in a matrix
- Find maximum element of each column in a matrix in C++
- Find maximum element of each row in a matrix in C++
- Find the maximum element of each row in a matrix using Python
- Python Program to find the Next Nearest element in a Matrix
- Program to find maximum non negative product in a matrix in Python
- Program to find out the cells containing maximum value in a matrix in Python
- PHP program to find the maximum element in an array
- Program to find smallest intersecting element of each row in a matrix in Python
- Program to find maximum element after decreasing and rearranging in Python
- Write a program in Go language to find the element with the maximum value in an array
- Program to find the minimum (or maximum) element of an array in C++
- C# program to find maximum and minimum element in an array\n
- C++ Program to Find Maximum Element in an Array using Binary Search
- Program to find maximum XOR with an element from array in Python

Advertisements