Program to find maximum element of each column in matrix

Program to find maximum element of each column in a matrix in C++,find the maximum value in each column of the given matrix. "The Coding Bro".

Maximum Element of Each Column

In this post we will be discuss the Program to find maximum element of each column in a matrix i.e., to find the maximum value in each column of the given matrix. Also, the required logic is discussed and the source code is written in C++ which is compiled successfully to provide the best results.

Problem Description

In a family, the people are arranged in rows and columns. Male persons in the families are arranged in a row and females are arranged in a column. Find the eldest women in each column. (Write a program to find the maximum element in each column of the matrix.)

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 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:

3

2

4 5

6 9

0 3

Sample Output:

6

9

Explaination :

In this we will be discussing the Program to find maximum element of each column in a matrix i.e., to find the maximum value in each column of the given matrix. This can be achieved by simple loop and conditional statement. Initialize the max variable to first element of each column. If there is only one element present in each column of the matrix then the loop did not execute and max hold the only present value in the matrix, thus that element becomes the maximum of each column. If matrix has more than one element, than loop executes and if any element found bigger than the previously assigned value, then that element becomes the largest.

Program to find maximum element of each column in matrix in C++

🐱‍🏍Logic to follow to come-up with the solution :

1. Declare the required sets of variables to use in the code.

2. Initialize the max variable to first element of each column.

3. If there is only one element present in each column of the matrix then the loop did not execute and max hold the only present value in the matrix, thus that element becomes the maximum of each column.

4. If matrix has more than one element, than loop executes and if any element found bigger than the previously assigned value, then that element becomes the largest.

5. At last maximum value of each column is displayed as the result output.

👨‍💻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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include <bits/stdc++.h>
using namespace std;

void largestInColumn(int mat[10][10], int rows, int cols)

{

    for (int i = 0; i < cols; i++)

    {

     int maxm = mat[0][i];

     for (int j = 1; j < rows; j++)

     {

        if (mat[j][i] > maxm)

         maxm = mat[j][i];

        }

        cout << maxm << endl;

    }

}

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];

  }

  }

    largestInColumn(mat, n, m);

    return 0;

}

 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 !!

Mayank Pathak is the editor and blogger at "The Coding Bro".He loves to codes, learn new technologies, and write the coding solutions and articles for the communities of learners, especiall…

Post a Comment

Please do not enter any spam link in the comments.
© THE CODING BRO. All rights reserved. Premium By The Coding Bro