find the minimum and maximum element in each column of a matrix

Program to find the minimum and maximum element in each column of a matrix is discussed here. Given a matrix, the maximum an minimum elements of each column of the matrix are displayed as output.

Maximum element in each column of the matrix

Input:

3   3  (Order of the matrix – number of rows and columns)

1    4    9
3    5    1
2    8    5

Output:

3
8
9

Algorithm to find the maximum element in each column of a matrix

• Input the order of the matrix.
• Input the matrix elements.
• For row = 0 to n-1
• Find the maximum element in the column and print it.

Program to find the maximum element in each column of a matrix is given below.

/* C Program to find the maximum element in each column of a matrix */
#include<stdio.h>

void maxi_col(int mat[][3], int m, int n)
{
int i,j;
for(i = 0; i < n; i++)
{
int max = mat[0][i];
for(j = 1; j < m; j++)
{
if(mat[j][i] > max)
{
max = mat[j][i];
}
}
printf(“%d\n”,max);
}
}
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}

maxi_col(mat1,m,n);
return 0;
}

/* C++ Program to find the maximum element in each column of a matrix */
#include<iostream>
#include<limits.h>

using namespace std;
const int n = 3;
const int m = 3;

void maxi_col(int mat[][3], int m, int n)
{
int i,j;
for(i = 0; i < n; i++)
{
int max = mat[0][i];
for(j = 1; j < m; j++)
{
if(mat[j][i] > max)
{
max = mat[j][i];
}
}
cout << max << endl;
}
}
int main()
{
int i, j;
int mat1[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}

maxi_col(mat1,m,n);
return 0;
}

// Java Program to find the maximum element in each column of a matrix

import java.util.*;
public class Main
{
public static void maxi_col(int mat[][], int m, int n)
{
int i,j;
for(i = 0; i < n; i++)
{
int max = mat[0][i];
for(j = 1; j < m; j++)
{
if(mat[j][i] > max)
{
max = mat[j][i];
}
}
System.out.println(max);
}
}

public static void main(String[] args)
{
int n, m;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the order of the matrix : “);
m = sc.nextInt();
n = sc.nextInt();
int[][] mat1 = new int[m][n];
System.out.print(“\nInput the matrix 1 elements : “);
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat1[i][j] = sc.nextInt();
}

maxi_col(mat1,m,n);
}
}

# Python program to find the maximum element in each column of the matrix

def maxi_col(mat):
for i in range(0,n):
maxi = mat[0][i]
for j in range(1,n):
if(mat[j][i] > maxi):
maxi = mat[j][i]
print(maxi)

mat1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
maxi_col(mat1)

Output:

Minimum element in each column of a matrix

Input:

3   3  (Order of the matrix – number of rows and columns)

1    4    9
3    5    1
2    8    5

Output:

1
4
1

Algorithm to find the minimum element in each column of a matrix

• Input the order of the matrix.
• Input the matrix elements.
• For row = 0 to n-1
• Find the minimum element in the column and print it.

Program to find the minimum element in each column of a matrix is given below.

/* C Program to find the minimum element in each column of a matrix */
#include<stdio.h>

void mini_col(int mat[][3], int m, int n)
{
int i,j;
for(i = 0; i < n; i++)
{
int min = mat[0][i];
for(j = 1; j < m; j++)
{
if(mat[j][i] < min)
{
min= mat[j][i];
}
}
printf(“%d\n”,min);
}
}
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}

mini_col(mat1,m,n);
return 0;
}

/* C++ Program to find the minimum element in each column of a matrix */
#include<iostream>
#include<limits.h>

using namespace std;
const int n = 3;
const int m = 3;

void mini_col(int mat[][3], int m, int n)
{
int i,j;
for(i = 0; i < n; i++)
{
int min = mat[0][i];
for(j = 1; j < m; j++)
{
if(mat[j][i] < min)
{
min = mat[j][i];
}
}
cout << min << endl;
}
}
int main()
{
int i, j;
int mat1[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}

mini_col(mat1,m,n);
return 0;
}

// Java Program to find the minimum element in each column of a matrix

import java.util.*;
public class Main
{
public static void mini_col(int mat[][], int m, int n)
{
int i,j;
for(i = 0; i < n; i++)
{
int min = mat[0][i];
for(j = 1; j < m; j++)
{
if(mat[j][i] < min)
{
min = mat[j][i];
}
}
System.out.println(min);
}
}

public static void main(String[] args)
{
int n, m;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the order of the matrix : “);
m = sc.nextInt();
n = sc.nextInt();
int[][] mat1 = new int[m][n];
System.out.print(“\nInput the matrix 1 elements : “);
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat1[i][j] = sc.nextInt();
}

mini_col(mat1,m,n);
}
}

# Python program to find the minimum element in each column of a matrix

def mini_col(mat):
for i in range(0,n):
mini = mat[0][i]
for j in range(1,n):
if(mat[j][i] < mini):
mini = mat[j][i]
print(mini)

mat1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
mini_col(mat1)

Output: