•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,133 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,765 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2832 | Replies: 6
![]() |
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Rep Power: 2
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);
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation:
Rep Power: 11
Solved Threads: 296
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)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
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.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation:
Rep Power: 11
Solved Threads: 296
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)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
•
•
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}
yaa sir u r right on run this code ans is 3 which is wrong.now i will try to slove this wiht my code.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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



Linear Mode