0% found this document useful (0 votes)
545 views5 pages

Java Saddle Point Matrix Program

This Java program finds the saddle point(s) of a matrix. It takes in a matrix size and values, identifies elements that are the minimum in their row and maximum in their column, and displays the saddle points. It also sorts the matrix diagonally and outputs the original and sorted matrices.

Uploaded by

Varun Sudarsanan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
545 views5 pages

Java Saddle Point Matrix Program

This Java program finds the saddle point(s) of a matrix. It takes in a matrix size and values, identifies elements that are the minimum in their row and maximum in their column, and displays the saddle points. It also sorts the matrix diagonally and outputs the original and sorted matrices.

Uploaded by

Varun Sudarsanan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

SADDLE POINT

2003 Qn. 3
import [Link].*;

class Saddle_Point

int A[][]=new int[20][20];

int N;

int sad[]=new int[20];

int sort[][]=new int[20][20];

int f=0;

public void input()throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter the order of the Array: " );

N=[Link]([Link]());

if(N>20)

[Link]("Limit exceeded ");

[Link](0);

[Link]("Enter the elements : ");

for(int i=0;i<N;i++)

for(int j=0;j<N;j++)

A[i][j]=[Link]([Link]());

sort[i][j]=A[i][j];
}

public void calc()

for(int i=0;i<N;i++)

for(int j=0;j<N;j++)

if(rowmin(i,j)&&colmax(i,j))

sad[f++]=A[i][j];

int temp;

for(int i=0;i<N;i++)

for(int j=0;j<N;j++)

if(A[j][j]>A[j+1][j+1])

temp=sort[j][j];

sort[j][j]=sort[j+1][j+1];

sort[j+1][j+1]=temp;

public boolean rowmin(int p, int q)

int m=A[p][q];

for(int i=0;i<N;i++)

if(m>A[p][i])

return false;

return true;
}

public boolean colmax(int p, int q)

int m=A[p][q];

for(int i=0;i<N;i++)

if(m<A[i][q])

return false;

return true;

public void display()

int i,j;

[Link]("N = "+N);

[Link]("ORIGINAL MATRIX ");

for(i=0;i<N;i++)

for(j=0;j<N;j++)

[Link](A[i][j]+" ");

[Link]();

[Link]("SADDLE POINT(s) : ");

for(i=0;i<f;i++)

[Link](sad[i]);

if(i==0)

[Link]("No Saddle Points");

[Link]("DIAGONALLY SORTED");
for(i=0;i<N;i++)

for(j=0;j<N;j++)

[Link](sort[i][j]+" ");

[Link]();

public void main()throws IOException

input();

calc();

display();

OUTPUT

Enter the order of the Array:

Enter the elements :

16

12

8
14

N=3

ORIGINAL MATRIX

4 16 12

2 8 14

136

SADDLE POINT(s) :

DIAGONALLY SORTED

4 16 12

2 8 14

136

You might also like