Thursday 22 November 2012

Saddle Number


/*BlueJ program on finding saddle number  

What is saddle number 

Saddle number is related with numeric 2 d array. The number which is lowest in a particular  row but at the same time the same number is greatest in its column. The size of 2 d array has  no relation with saddle number. It may be of any number of rows and any number of columns.  Again if the conditions of saddle number do not match in a 2 d array, there will be no saddle number in it.  How to proceed on this saddle number program??  2 d array and nested loop is required for this saddle number finding program. Firstly store  the values in the 2 d array. Next step is to find the saddle number. Find out the minimum value  in a row and check whether that particular value is maximum in its column. If the checking confirms  that it is the maximum value in that column then saddle number is found. Repeat the above steps for all the rows.*/ 

import java.io.*; 
class Saddle 
{ 
int arr[][]=new int[4][4]; 
int saddle,i,j; 
InputStreamReader reader =new InputStreamReader(System.in); 
BufferedReader br= new BufferedReader(reader); 
public void takeNumbers ()throws Exception 
{ 
for(i=0;i<4;i++) 
{ 
for(j=0;j<4;j++) 
{ 
System.out.println("Number:"); 
arr[i][j]=Integer.parseInt(br.readLine().trim()); 
} 
} 
System.out.println("\nThe list is as follows:\n"); 
for(i=0;i<4;i++) 
{ 
for(j=0;j<4;j++) 
{ 
System.out.print(arr[i][j]+" "); 
} 
System.out.println(); 
} 
} 
public void showResult () 
{ 
int flag=0,k,minr=0,minc=0; 
for(i=0;i<4;i++) 
{ 
for(j=0;j<4;j++) 
{ 
if(j==0) 
{ 
minr=arr[i][j]; 
minc=j; 
} 
else if(arr[i][j]< minr) 
{ 
minr=arr[i][j]; 
minc=j; 
} 
} 
for(k=0;k<4;k++) 
{ 
if(minrarr[k][minc]) 
break; 
} 
if(k==4) 
{ 
flag=1; 
saddle=minr; 
} 
} 
if(flag!=0) 
System.out.println("Saddle number="+saddle); 
else 
System.out.println("No saddle number in the list"); 
} 
public static void main (String args[])throws Exception 
{ 
Saddle obj=new Saddle (); 
obj.takeNumbers (); 
obj.showResult (); 
} 
} 


/*Technical analysis of the saddle number program . I think storing the values in 2 d array and displaying them is clear to all.   The values are displayed so that user can check manually the correctness of the program.   Within the function showResult() the job of searching the saddle number is performed.   The outer loop contains two inner loops within its body. The first inner loop is used to   find the lowest number in that row and the other inner loop checks whether this lowest number  is the greatest number in that particular column. A flag variable is used to check if saddle number is present in the 2 d array.*/ 

2 comments: