Tuesday , 26 March 2019
Latest Posts
Home » Array Related Programs » Java program to find Saddle Point in a square matrix

# Java program to find Saddle Point in a square matrix

#### Question:

Write a program to declare a square matrix A[ ][ ] of order ‘n’. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:

(i) Output the original matrix.
(ii) Find the SADDLE POINT for the matrix. If the matrix has no saddle point, output the message “NO SADDLE POINT”.

[Note: A saddle point is an element of the matrix such that it is the minimum element for the row to which it belongs and the maximum element for the column to which it belongs. Saddle point for a given matrix is always unique.]

Example: In the Matrix
4 5 6
7 8 9
5 1 3

Saddle point = 7 because it is the minimum element of row 2 and maximum element of column 1

This question came in Question 3 of ISC 2003 Practical Examination.

#### Programming Code:

```/**
* The class SaddlePoint inputs a matrix of n*n size and finds its
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2003 Question 3 (Practical)
*/

import java.io.*;
{
public static void main(String args[])throws IOException
{
System.out.print("Enter the order of the matrix : ");
int A[][]=new int[n][n];
System.out.println("Inputting the elements in the matrix");
System.out.println("******************************"); // Ignore these. They are just for styling
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("Enter Element at &#91;"+i+"&#93;&#91;"+j+"&#93; : ");
}
}

/* 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&#91;i&#93;&#91;j&#93;+"\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&#91;i&#93;&#91;0&#93;; // Initializing min with first element of every row
x = 0;
for(int j=0;j<n;j++)
{
if(A&#91;i&#93;&#91;j&#93;<min)
{
min = A&#91;i&#93;&#91;j&#93;;
x = j; // 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&#91;0&#93;&#91;x&#93;; // Initializing max with first element of that column
for(int k=0;k<n;k++)
{
if(A&#91;k&#93;&#91;x&#93;>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("********************");
f=1;
}
}

if(f==0)
{
System.out.println("********************");
System.out.println("********************");
}
}
}```

## Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.