943,846 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6495
  • Java RSS
Apr 7th, 2007
0

to secomd smallest number in array

Expand 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);
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
Apr 7th, 2007
1

Re: to secomd smallest number in array

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)
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Apr 7th, 2007
0

Re: to secomd smallest number in array

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
Reputation Points: 19
Solved Threads: 4
Light Poster
MacGyver Orca is offline Offline
39 posts
since Jan 2007
Apr 8th, 2007
0

Re: to secomd smallest number in array

no i gave all diff values
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
Apr 8th, 2007
0

Re: to secomd smallest number in array

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

Click to Expand / Collapse  Quote originally posted by pavani2006 ...
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);
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
ajay_tabbu is offline Offline
52 posts
since Feb 2007
Apr 8th, 2007
0

Re: to secomd smallest number in array

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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Apr 8th, 2007
0

Re: to secomd smallest number in array

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
ajay_tabbu is offline Offline
52 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: my project is IDE for java
Next Thread in Java Forum Timeline: File scaning help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC