# Program to find the minimum and maximum element in each row of a matrix

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

##### Maximum element in each row 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:

9
5
8

## Algorithm to find the maximum element in each row 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 row and insert the element in an array.
• Print the array.

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

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

void display(int result[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf(“%d “, result[i]);
}
}

void maxi_row(int mat[][3], int m, int n)
{
int i = 0, j;
int max = 0;
int result[m];
while (i < m)
{
for ( j = 0; j < n; j++)
{
if (mat[i][j] > max)
{
max = mat[i][j];
}
}
result[i] = max;
max = 0;
i++;

}
display(result, m);
}
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_row(mat1,m,n);
return 0;
}

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

using namespace std;
const int n = 3;
const int m = 3;
void display(int result[], int n)
{
int i;
for(i = 0; i < n; i++)
{
cout << result[i] << ” “;
}
}

void maxi_row(int mat[][3], int m, int n)
{
int i = 0, j;
int max = 0;
int result[m];
while (i < m)
{
for ( j = 0; j < n; j++)
{
if (mat[i][j] > max)
{
max = mat[i][j];
}
}
result[i] = max;
max = 0;
i++;

}
display(result, m);
}
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_row(mat1,m,n);
return 0;
}

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

import java.util.*;
public class Main
{
public static void display(int result[], int n)
{
int i;
for(i = 0; i < n; i++)
{
System.out.print(result[i] + ” “);
}
}

public static void maxi_row(int mat[][], int m, int n)
{
int i = 0, j;
int max = 0;
int[] result = new int[m];
while (i < m)
{
for ( j = 0; j < n; j++)
{
if (mat[i][j] > max)
{
max = mat[i][j];
}
}
result[i] = max;
max = 0;
i++;

}
display(result, m);
}

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_row(mat1,m,n);
}
}

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

def maxi_row(arr):

no_of_rows = len(arr)
no_of_column = len(arr[0])

for i in range(no_of_rows):

max1 = 0
for j in range(no_of_column):
if arr[i][j] > max1 :
max1 = arr[i][j]

print(max1)

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

Output:

##### Minimum element in each row 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
1
2

## Algorithm to find the minimum element in each row 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 row and insert the element in an array.
• Print the array.

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

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

void display(int result[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf(“%d “, result[i]);
}
}

void mini_row(int mat[][3], int m, int n)
{
int i = 0, j;
int min = INT_MAX;
int result[m];
while (i < m)
{
for ( j = 0; j < n; j++)
{
if (mat[i][j] < min)
{
min = mat[i][j];
}
}
result[i] = min;
min = INT_MAX;
i++;

}
display(result, m);
}
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_row(mat1,m,n);
return 0;
}

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

using namespace std;
const int n = 3;
const int m = 3;
void display(int result[], int n)
{
int i;
for(i = 0; i < n; i++)
{
cout << result[i] << ” “;
}
}

void mini_row(int mat[][3], int m, int n)
{
int i = 0, j;
int min = INT_MAX;
int result[m];
while (i < m)
{
for ( j = 0; j < n; j++)
{
if (mat[i][j] < min)
{
min = mat[i][j];
}
}
result[i] = min;
min = INT_MAX;
i++;

}
display(result, m);
}
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_row(mat1,m,n);
return 0;
}

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

import java.util.*;
public class Main
{
public static void display(int result[], int n)
{
int i;
for(i = 0; i < n; i++)
{
System.out.print(result[i] + ” “);
}
}

public static void maxi_row(int mat[][], int m, int n)
{
int i = 0, j;
int min = Integer.MAX_VALUE;
int[] result = new int[m];
while (i < m)
{
for ( j = 0; j < n; j++)
{
if (mat[i][j] < min)
{
min = mat[i][j];
}
}
result[i] = min;
min = Integer.MAX_VALUE;
i++;

}
display(result, m);
}

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_row(mat1,m,n);
}
}

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

def mini_row(arr):

no_of_rows = len(arr)
no_of_column = len(arr[0])

for i in range(no_of_rows):

min1 = 99999
for j in range(no_of_column):
if arr[i][j] < min1 :
min1 = arr[i][j]

print(min1)

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

Output: