Saddle point program

Write a program in Java to input the size of a square matrix from the user. Then allow the user to fill the matrix with integers. Now do the following:

  1. Display the original matrix.
  2. Display the saddle point of the matrix if one exists, otherwise display “No saddle point”.

saddle point of a matrix is the element in the matrix which is the smallest in its row, and the largest in its column.

Example:
INPUT:
N = 3

1 2 3

4 5 6

7 8 9

OUTPUT:

Original Matrix:

1 2 3

4 5 6

7 8 9

Saddle point = 7.

//This program finds the lowest element in the row and checks whether it value is the highest in that particular column
import java.io.*;
class SaddlePoint
{
public static void main() throws IOException
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the order of the matrix : ");
int n=Integer.parseInt(br.readLine());
int A[[]=new int[n][n];
System.out.println("Inputting the elements in the
matrix");
System.out.println("******"); 
for(int i=0;i<n;i++)
{
for(int j=0; j<n; j++)
{
System.out.print("Enter Element at ("+i+"]["+j+"]
A[i][j]=Integer.parseInt(br.readLine());
/* Print the Original Matrix */
**
");
System.out.println("**
System.out.println("The Original Matrix is");
for(int i=0;i<n;i++)
{
for(int j=0; j<n; j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int max. min, X, F=0;
for(int i=0;i<n;i++)
/* Finding the minimum element of a row */
min = A[i][0]; // Initializing min with first element of each row
x = 0;
for(int j =0; j<n; j++)
{
if(A[i][j]<min)
min = A[i][j]:
X = ji / Saving the column position of the
minimum element of the row
}
}
/* Finding the maximum element in the column
* corresponding to the minimum element of row */
max = A[O][x]; // Initializing max with first element of that column
for(int k=0; k<n;k++)
{
if(A[k][x]>max)
{
max = A[k][x];
}
/* If the minimum of a row is same as maximum of the corresponding column. then, we have that element as the Saddle point */
if(max==min)
{
System.out.println("**
System.out.println("Saddle point = "+max);
**");
System.out.println("* f=1;***"):
}
if(f==0)
{
System.out.println("**
**");
System.out.println("No saddle point");
***");
System.out.println("
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here