Find maximum and minimum in a matrix
C program to find maximum and minimum in a matrix.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int mat[MAX][MAX];
int row, col;
int i, j, max, min;
printf("Enter the number of rows\t:");
scanf("%d",&row);
printf("Enter the number of columns\t:");
scanf("%d",&col);
printf("Enter the elements\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Element [%d][%d]\t:",i,j);
scanf("%d",&mat[i][j]);
if(i==0 && j==0)
{
max = mat[0][0];
min = mat[0][0];
}
if(min > mat[i][j])
{
min = mat[i][j];
}
if(max < mat[i][j])
{
max = mat[i][j];
}
}
}
printf("\n\nMatrix entered is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
printf("\n\nMaximum Value\t:%d",max);
printf("\nMinimum Value\t:%d",min);
getch();
}
Output
Enter the number of rows :3
Enter the number of columns :4
Enter the elements
Element [0][0] :23
Element [0][1] :34
Element [0][2] :45
Element [0][3] :54
Element [1][0] :32
Element [1][1] :12
Element [1][2] :45
Element [1][3] :67
Element [2][0] :87
Element [2][1] :56
Element [2][2] :43
Element [2][3] :32
Matrix entered is
23 34 45 54
32 12 45 67
87 56 43 32
Maximum Value :87
Minimum Value :12