i want to find the second highest number in arrat .i wrote the code 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);
second smallest number in arrayurgent
Page 1 of 1
1 Replies - 6468 Views - Last Post: 07 April 2007 - 08:19 AM
Replies To: second smallest number in array
#2
Re: second smallest number in array
Posted 07 April 2007 - 08:19 AM
hi, when you have the highest number max all you have to do is
copy it's value to some other variable like temp
and set the a[i] which is the max to a value a[i]=0 and max=0
then start the search for the max again so you'll get the second highest number with max.
copy it's value to some other variable like temp
and set the a[i] which is the max to a value a[i]=0 and max=0
then start the search for the max again so you'll get the second highest number with max.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|