| | |
to secomd smallest number in array
![]() |
•
•
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 4: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
Views: 5250 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener add android applet application arguments array arraylist arrays c# chat class classes client code compile compiler component convert coordinates data database desktop draw eclipse error event exception fast file filei/o forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework homeworkassignment html ide image images inheritance input interface j2me java jframe jpanel jtextarea lazy linked linked-list list loop looping main method methods mobile netbeans newbie node number object oracle output page pattern phone pixel print problem programming read regex remove robot scanner search server set sms socket software sort source sql string swing system text thread time timer tree url web windows






