C++ program to display largest element in each row in 2-D Array

Display Largest Element in each row in C++


In this article, we will discuss about largest element in each row in 2-D Array . Array has n-rows and m-columns , here our task is to write a program to Display the largest element in each row in a 2-D Array in C++.

Example :

  • Sample input: a=[[1,2,3],[6,5,4],[8,9,7]]
  • Sample output: 3 6 9
  • Explanation : In first row maximum element is 3
                            In second row maximum element is 6
                            In third row maximum element is 9
maximum element in each row in C++

Algorithm :

  • Take the row and column of a matrix as input and store them in r and c variables respectively.
  • Declare a r*c 2D array.
  • Take the input of the 2D array from the user.
  • Declare maxi variable and initialize it with the Minimum value of Integer.
  • Traverse each element of a row and update the max variable according to the element.
  • After traversing a row print the maxi element
  • At the end all the max element of each row get printed.

C++ Code based on above algorithm :

using namespace std;

int main(){

int r, c;

cout<<"Enter the number od rows and columns :";
cin>> r >> c;

int a[r][c];

cout<<"\nEnter the elements of matrix :";

for(int i=0; i<r; i++){

for(int j=0; j<c; j++)

for(int i=0; i<r; i++){

int maxi = INT_MIN;

for(int j=0; j<c; j++)
maxi = max(a[i][j], maxi);


return 0;
Output :

Enter the number of rows and columns : 3 3

Enter the elements of matrix : 

3 5 8

10 12 6

0 2 7