bluej Programs
6.Write a program to enter a matrix of size m X n where m and n must be greater than 2 and less than 6.Print the saddle point if any.A saddle point in a matrix is an element which is minimum in its row and maximum in its column.
import java.util.*;
class saddle_point
{
Scanner sc=new Scanner(System.in);
int ar[][],m,n;
void input()
{
System.out.println("Enter the total number of rows and columns:");
m=sc.nextInt();
n=sc.nextInt();
if(m<=2 || m>=6 ||n<=2 || n>=6)
{
System.out.println("Wrong Input");
System.exit(0);
}
ar=new int[m][n];
for(int i=0;i<m;i++)
{
System.out.println("Enter "+n+" number of elements");
for(int j=0;j<n;j++)
{
ar[i][j]=sc.nextInt();
}
}
}
void display()
{
System.out.println("The Original array is:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
}
void saddle_check()
{
int mn,c=0,mx,flg=0;
for(int i=0;i<m;i++)
{
mn=ar[i][0];
for(int j=0;j<n;j++)
{
if(mn>ar[i][j])
{
mn=ar[i][j];
c=j;
}
}
mx=ar[0][c];
for(int k=0;k<m;k++)
{
if(mx<ar[k][c])
{
mx=ar[k][c];
}
}
if(mx==mn)
{
System.out.println("Saddle point is:"+mx);
flg=1;break;
}
}
if(flg==0)
System.out.println("No Saddle point is present");
}
void main()
{
saddle_point ob=new saddle_point();
ob.input();
ob.display();
ob.saddle_check();
}
}
Output:
Enter the total number of rows and columns: 3 3 Enter 3 number of elements 9 4 7 Enter 3 number of elements 6 7 8 Enter 3 number of elements 9 8 10 The Original array is: 9 4 7 6 7 8 9 8 10 Saddle point is:8
ADVERTISEMENT
