i really need help with this program i am finding a saddle number of a 2-d array which is the min value in a particular column and maximum value in its row.......and my error with the code is arrayindexoutofboundexception
eg
input num=4
7 6 9 10
5 3 4 6
8 2 4 8
9 5 4 8
output 6

import java.io.*;
public class sp
{
int i,saddle,num;int arr[][]=new int[num][num];BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takenum()throws IOException
{

int j;
System.out.println("ENTER SIZE");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
System.out.print("NUMBERS");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("LIST");
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public void showresult()throws IOException
{
int j,flag=0,minr=0,minc=0,k;
for(i=0;i<num;i++)
{
for(j=0;j<num;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<num;k++)
{
if(minr<arr[k][minc])
{
break;
}}
if(k==num)
{
flag=1;
saddle=minr;
}
}
if(flag!=0)
System.out.print("SP="+saddle);
else
System.out.print("NSP");
}
public void main()throws IOException
{
sp obj=new sp();
obj.takenum();obj.showresult();}}

Recommended Answers

All 2 Replies

The main method should be written as follows:

public static void main(String args[])throws IOException
{
sp obj=new sp();
obj.takenum();obj.showresult();
}

thanx a lot....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.