User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,133 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,765 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2832 | Replies: 6
Reply
Join Date: Feb 2007
Posts: 20
Reputation: pavani2006 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
pavani2006 pavani2006 is offline Offline
Newbie Poster

to secomd smallest number in array

  #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);
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: to secomd smallest number in array

  #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)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jan 2007
Location: Earth....
Posts: 39
Reputation: MacGyver Orca is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
MacGyver Orca's Avatar
MacGyver Orca MacGyver Orca is offline Offline
Light Poster

Re: to secomd smallest number in array

  #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  
Join Date: Feb 2007
Posts: 20
Reputation: pavani2006 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
pavani2006 pavani2006 is offline Offline
Newbie Poster

Re: to secomd smallest number in array

  #4  
Apr 8th, 2007
no i gave all diff values
Reply With Quote  
Join Date: Feb 2007
Location: india
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Rep Power: 2
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

  #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  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: to secomd smallest number in array

  #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 4:36 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Feb 2007
Location: india
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Rep Power: 2
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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC