This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
lo : Smallest. Write a program that allows a user to enter a series of positive integers with a -99 to signal the end of the series. The program should then display the smallest of the numbers in the series. Do *not* use a bogus value for the smallest to begin the program. Use the first value in the data list (if any) to initialize your smallest. This program must have only *one* programmer-defined method other than main which outputs user directions to the console about how the program works (and that -99 is to be used to end the input). Note that an empty list is possible and should not confuse your program! After displaying directions to the console, use GUI for the remaining input and output. Try out your program on the following lists:
10, 3, 8, 2, -99 (lo = 2) -99 (ouput should be: "no numbers, no lo") 4, 6, 1, 106, 22, -99 (lo = 1)
So far, I've established that...
loop, probably do/while get input from user, input if input != "-99", set list (array) = input
if input == "-99" set list = input (makes it easy to terminate your loop through the array and to tell if it is otherwise emtpy) sort through array, comparing two elements of the array, keep lowest and compare to next available, till -99 found print as necessary
- This is what my friend has told me so far, but I'm not sure what he means by 'set list (array) = unput.
Unless you need to produce the list of inputs at the end, you don't need to use an array or List at all. Just use a single int variable "min". On the first input, set "min" to be the number you read. For each additional input, if it's not -99, then set min to the new number if and only if the number is less than min. If it is -99, then output min -- unless -99 is the first number, in which case you'd want to print that error message.
ernest do you have AIM? I understand exactly what you are saying and that IS what I need to do.. but I'm having trouble setting up the loop statement. Do I use while or do-while? and do I put the prompt for input (JOPtionPane...) in the loop body? I just learned about these loops today, so it's very new to me.
Joined: Oct 12, 2006
Please don't post in multiple forums, it wastes peoples' time.