# second smallest number in array

Page 1 of 1

## 1 Replies - 7103 Views - Last Post: 07 April 2007 - 08:19 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26080&amp;s=da7d157e6d88ef4ef619497d5e9fda94&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pavani2006

• New D.I.C Head

Reputation: 2
• Posts: 15
• Joined: 22-February 07

# second smallest number in array

Posted 07 April 2007 - 07:56 AM

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

Is This A Good Question/Topic? 1

## Replies To: second smallest number in array

### #2 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

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