1 Replies - 7006 Views - Last Post: 07 April 2007 - 08:19 AM Rate Topic: ***** 1 Votes

#1 pavani2006  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1