to secomd smallest number in array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 20
Reputation: pavani2006 is an unknown quantity at this point 
Solved Threads: 0
pavani2006 pavani2006 is offline Offline
Newbie Poster

to secomd smallest number in array

 
0
  #1
Apr 7th, 2007
i want to find the second smallest number ib array but it is displaying second highest number
int max=0,second=0;
if(a[0]>a[1])
{
max=a[0];
second=a[1];
}
else
{
max=a[1];
second=a[0];
}
for( i=2;i<n;i++)
{
if(a[i]>=max)
{
second=max;
max=a[i];
}
else

if(a[i]>second)
second=a[i];

}
System.out.println("the second smallest nubmer is"+second);
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,212
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: to secomd smallest number in array

 
1
  #2
Apr 7th, 2007
I would suggest to sort the array first and then it is simple just reguest number on second possition. Also keep in mind that you may have first two number of same value so the second smallest should be on 3rd position(but that is just extra feature of your program)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 39
Reputation: MacGyver Orca is an unknown quantity at this point 
Solved Threads: 4
MacGyver Orca's Avatar
MacGyver Orca MacGyver Orca is offline Offline
Light Poster

Re: to secomd smallest number in array

 
0
  #3
Apr 7th, 2007
Your instructor could also be extra devious and give you an array where all the indecies hold the same value. In which case the second smallest number would still be that value
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: pavani2006 is an unknown quantity at this point 
Solved Threads: 0
pavani2006 pavani2006 is offline Offline
Newbie Poster

Re: to secomd smallest number in array

 
0
  #4
Apr 8th, 2007
no i gave all diff values
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Solved Threads: 0
ajay_tabbu's Avatar
ajay_tabbu ajay_tabbu is offline Offline
Junior Poster in Training

Re: to secomd smallest number in array

 
0
  #5
Apr 8th, 2007
try this code.i have change few thing in u r code


int min=0,second=0;
if(a[0]<a[1])
{
min=a[0];
second=a[1];
}
else
{
min=a[1];
second=a[0];
}
for(int i=2;i<9;i++)
{
if(a[i]<=min)
{
second=min;
min=a[i];
}
else
{
if(a[i]<second)
second=a[i];
}
}
System.out.println("the second smallest nubmer is "+second);

Originally Posted by pavani2006 View Post
i want to find the second smallest number ib array but it is displaying second highest number
int max=0,second=0;
if(a[0]>a[1])
{
max=a[0];
second=a[1];
}
else
{
max=a[1];
second=a[0];
}
for( i=2;i<n;i++)
{
if(a[i]>=max)
{
second=max;
max=a[i];
}
else

if(a[i]>second)
second=a[i];

}
System.out.println("the second smallest nubmer is"+second);
i m oblised for all member of daniweb.as helping me every time.this is best site for those who want to learn themself.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,212
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: to secomd smallest number in array

 
0
  #6
Apr 8th, 2007
Already I can see problem here, in both cases if/else statement will be useless if first two numbers will be some of largest. You are expecting smallest number to be on one of first two positions in array.
Consider following array and think about it again
arr = {4, 5, 1, 3, 2}
Last edited by peter_budo; Apr 8th, 2007 at 5:36 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Solved Threads: 0
ajay_tabbu's Avatar
ajay_tabbu ajay_tabbu is offline Offline
Junior Poster in Training

Re: to secomd smallest number in array

 
0
  #7
Apr 8th, 2007
Originally Posted by peter_budo View Post
Already I can see problem here, in both cases if/else statement will be useless if first two numbers will be some of largest. You are expecting smallest number to be on one of first two positions in array.
Consider following array and think about it again
arr = {4, 5, 1, 3, 2}
yaa sir u r right on run this code ans is 3 which is wrong.now i will try to slove this wiht my code.
thanking u
i m oblised for all member of daniweb.as helping me every time.this is best site for those who want to learn themself.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC