Tuesday , 17 January 2017
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
* saddle point if any
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2003 Question 3 (Practical)
*/

import java.io.*;
class SaddlePoint
{
   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("******************************"); // 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 ["+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++)
       {
           /* Finding the minimum element of a row */
           min = A[i][0]; // Initializing min with first element of every row
           x = 0;
           for(int j=0;j<n;j++)
           {
                if(A[i][j]<min)
                {
                    min = A[i][j];
                    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[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 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("********************");
       }
   }
}

Output:

saddle point java isc icse

Check Also

ISC 2016 Computer Science Solution + Examiner’s Comments – From the Council

Solution of ISC 2016 Computer science Paper as provided by the Council for the Indian School Certificate Examinations.

2 comments

  1. can u please say how to find out the sum of the elements of the boundary elements

Leave a Reply

Your email address will not be published. Required fields are marked *