473,414 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

compute the two smallest numbers

i am writing a simple java program but everything i do it seems to not work. i need to find the smallest and second smallest number in a set. please help me understand what i have done wrong.

heres what i figured(but doesn't work fully):

Expand|Select|Wrap|Line Numbers
  1. public class TwoSmallest
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.     int size = IO.readInt();
  6.     if (size <= 0)
  7.         {
  8.         IO.reportBadInput();
  9.         return;
  10.         }
  11.     int counter = 1;
  12.     double number = IO.readDouble();
  13.     double smallest  = 100;
  14.     double smaller = 0;
  15.     do
  16.         {
  17.         if (number < smallest)
  18.             {
  19.             smallest = number;
  20.             counter = counter++;
  21.             }
  22.         else if (number > smallest && smallest < smaller)
  23.             {
  24.             smaller = number;
  25.             counter = counter++;
  26.             }
  27.         else
  28.             {
  29.             counter = counter++;
  30.             continue;
  31.             }
  32.         }
  33.     while (counter <= size);
  34.     IO.outputDoubleAnswer(smallest);
  35.     IO.outputDoubleAnswer(smaller);
  36.     return;
  37.     }
  38. }
Oct 1 '07 #1
6 4416
Ganon11
3,652 Expert 2GB
First of all, setting 100 to smallest is a messy way of doing this program. (Additionally, it won't work - consider if the user enters 101, 102, 103...200. Then your program thinks 0 is the smallest, and 100 is the second smallest, both of which were never actually entered by the user.) Consider the basic case, when the user enters only two numbers. Then the smallest number is, well, the smaller of these two, and the second smallest is the larger. If you take care of this simple case before moving onto the 3rd, 4th, 5th... number the user enters, your program will be much better.

Next, why are you using a do...while loop? You know exactly how many times the loop should execute, so use a for...loop.

Now, consider some point in the middle of your loop. Your user has entered some number of values (say 5). At this point, smallest = 2, and smaller = 5. Now, the user enters 0. 0 < smallest, so smallest should now = 0. Your code does this. But what is smaller? smaller is certainly not 5, it should be 1, right? Your logic does not account for this.

So now we have smallest = 0, and smaller = 2. Now the user enters 1. 1 > smallest, so we don't have to change that. But 1 < smaller, so we have to change that value - smaller should now be 1.

So now smallest = 0, and smaller = 1. Suppose the user enters 10. Well, 10 > smallest, and 10 > smaller, so you don't have to do anything.

Now you have the general behavior for any possible case - you should be able to code this pretty easily.
Oct 1 '07 #2
JosAH
11,448 Expert 8TB
[quote=yokaygirl143]i am writing a simple java program but everything i do it seems to not work. i need to find the smallest and second smallest number in a set. please help me understand what i have done wrong.

Why not copy your entire set to a TreeSet and take the first two elements?

kind regards,

Jos
Oct 2 '07 #3
Ganon11
3,652 Expert 2GB
[quote=JosAH]
i am writing a simple java program but everything i do it seems to not work. i need to find the smallest and second smallest number in a set. please help me understand what i have done wrong.

Why not copy your entire set to a TreeSet and take the first two elements?

kind regards,

Jos
Because it would be less efficient?

Inserting all elements: O(n log n)
Getting the first two: At best, O(log n), at worst, O(n)

Doing it this way: O(n)
Oct 2 '07 #4
JosAH
11,448 Expert 8TB
[quote=Ganon11]

Because it would be less efficient?

Inserting all elements: O(n log n)
Getting the first two: At best, O(log n), at worst, O(n)

Doing it this way: O(n)
I know, I know; but the OP doesn't have the slightest idea how to solve this now.
An inefficient solution might start a little idea (as in 'the next step').

kind regards,

Jos
Oct 2 '07 #5
Ganon11
3,652 Expert 2GB
[quote=JosAH]

I know, I know; but the OP doesn't have the slightest idea how to solve this now.
An inefficient solution might start a little idea (as in 'the next step').

kind regards,

Jos
True, functionality is more important than efficiency, but since I'm in a class that focuses on efficiency, it's the first response that came to mind. Have to stay on top of these things when I have an exam on Thursday.
Oct 2 '07 #6
JosAH
11,448 Expert 8TB
[quote=Ganon11]

True, functionality is more important than efficiency, but since I'm in a class that focuses on efficiency, it's the first response that came to mind. Have to stay on top of these things when I have an exam on Thursday.
My entire professional/mathematical life revolves around big-oh's ;-) Best of luck
with your exams.

kind regards,

Jos
Oct 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

52
by: Dick Moores | last post by:
I need to figure out how to compute pi to base 12, to as many digits as possible. I found this reference, <http://mathworld.wolfram.com/Base.html>, but I really don't understand it well enough....
2
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
13
by: Peter Ammon | last post by:
I have a floating point number. I'd like to get the nearest floating point number that is larger or smaller than the given number. I investigated FLT_EPSILON but it only seems to be useful if the...
11
by: gouqizi.lvcha | last post by:
Hi, All: I wonder what is the smallest positive double numbers in C in 32 bit CPU? Rick
4
by: MBeckford05 | last post by:
Hello, I am trying to write java program to input three integer numbers compare them and display the largest and Smallest number. I have thought about the problem I would need to use a comparison...
2
by: dolcetheking | last post by:
i got the Fibonacci Numbers algorithm : #include <iostream> using namespace std; int fib (int n) { if (n<3) return (1); else
2
by: milirica | last post by:
I have a code, where I should compute the App.Compute Time, for n=1,5,10,20,30,40,50.Is there any GOOD EXPERT that can make this clear to me ? If yes than PLease write here The code is: #include...
9
by: tom | last post by:
Hi! How can I determine the smallest and largest values of numeric types (for example int) possible in my system? I think there exists a function for this task but I don't know it.
8
by: Sunny | last post by:
Hi, do someone know, How we can find the smallest distance between a bunch of lat 7 long? Like I have 10 Latitude & Longitude. -73.924598,40.879010 -73.924506,40.878978 -73.924506,40.878978...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.