Java Program To Find Saddle Point In Square Matrix
Chapter:
Math Class
Last Updated:
30-05-2016 18:28:46 UTC
Program:
/* ............... START ............... */
import java.io.*;
public class JavaSaddlePoint {
public static void main(String args[]) 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());
}
}
/* Printing 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++) {
min = A[i][0];
x = 0;
for (int j = 0; j < n; j++) {
if (A[i][j] < min) {
min = A[i][j];
x = j;
}
}
/*
* Finding the maximum element in the column corresponding to the
* minimum element of row
*/
max = A[0][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 (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("********************");
}
}
}
/* ............... END ............... */
Output
Enter the order of the matrix : 3
Inputting the elements in the matrix
******************************
Enter Element at [0][0] : 2
Enter Element at [0][1] : 4
Enter Element at [0][2] : 5
Enter Element at [1][0] : 4
Enter Element at [1][1] : 8
Enter Element at [1][2] : 9
Enter Element at [2][0] : 2
Enter Element at [2][1] : 4
Enter Element at [2][2] : 4
******************************
The Original Matrix is
2 4 5
4 8 9
2 4 4
********************
Saddle point = 4
********************
Tags
Java Program To Find Saddle Point In Square Matrix, Java, Math