| | |
to secomd smallest number in array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
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);
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 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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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);
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);
•
•
•
•
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.
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
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
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}
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.
![]() |
Similar Threads
- Insert a number in an array (C++)
- help with inserting a number into an array (C++)
- Second smallest number? (C++)
Other Threads in the Java Forum
- Previous Thread: my project is IDE for java
- Next Thread: File scaning help
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






