# Find maximum element of each column in a matrix in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

Consider we have a matrix, our task is to find the maximum element of each column of that matrix and print them. This task is simple. For each column, reset the max, and find the max element, and print it. Let us see the code for better understanding.

## Example

#include<iostream>
#define MAX 10
using namespace std;
void largestInEachCol(int mat[][MAX], int rows, int cols) {
for (int i = 0; i < cols; i++) {
int max_col_element = mat[i];
for (int j = 1; j < rows; j++) {
if (mat[j][i] > max_col_element)
max_col_element = mat[j][i];
}
cout << max_col_element << endl;
}
}
int main() {
int row = 4, col = 4;
int mat[][MAX] = { { 3, 4, 1, 81 },
{ 1, 84, 9, 11 },
{ 23, 7, 21, 1 },
{ 2, 1, 44, 5 }
};
largestInEachCol(mat, row, col);
}

## Output

23
84
44
81