smallest number and largest number using while and if statements
hi i am having some problems on a basic program. i have to ask the user how many numbers they want to input and then based on this i have to find the largest and smallest numbers of the list. i can only use if statements and while statements. i have done the first part but i don’t understand how to begin the other part. my code so far is:
package whilesmalllarge;
import TerminalIO.KeyboardReader;
public class Smalllarge {
public static void main(String[] args) {
KeyboardReader reader = new KeyboardReader();
double usernumber= 0 ;
double i = 0;
double count;
count =reader.readInt(" How long is the list?");
while( i < count){
usernumber = reader.readInt(" input a number:");
i++;}
a test run:
How long is the list?3
input a number:3
input a number:5
input a number:4
Now it just has to say which number is the biggest and which one is the smallest
Re: smallest number and largest number using while and if statements
You're on the rigth track; now you have to fill in the details; now you have something like this:
Code:
while (i < count) {
read the input into usernumber
i++;
}
Suppose you have two numbers 'min' and 'max'; they contain the smallest and largest values of all numbers read so far. You have to update those numbers (if necessary) after you have read one more number; so in the body of your while loop you have to make the 'loop invariant' true again. If the newly read number is smaller than 'min', you have to update 'min'; if it is larger than 'max', you have to update 'max', so that the loop invariant is true again ...
kind regards,
Jos
Re: smallest number and largest number using while and if statements
You can't write the code until you have a "plan of attack". Suppose you were presented with a bunch of numbers one at a time and knew that at the end you were going to be asked which was the smallest. How would you do it?
In particular, what would you have to remember?
In code whatever it is that you had to remember would be a variable, and you would update it each time around the while loop.
[Edit] slow... ;(
Re: smallest number and largest number using while and if statements
sorry my teacher never said how you should do this. i was in cp intro to programming and then i went into java honors.i have looked at a couple of pos so would i like insert
highest = Integer.MAX_VALUE;
lowest = Integer.MIN_VALUE;
into the while statement?
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
vicu1
sorry my teacher never said how you should do this. i was in cp intro to programming and then i went into java honors.i have looked at a couple of pos so would i like insert
highest = Integer.MAX_VALUE;
lowest = Integer.MIN_VALUE;
into the while statement?
Don't make wild guesses; suppose someone hands you over a bunch of numbers, one by one; also suppose that someone else passes by and asks you "what is the largest number so far?"; what do you have to do to be able to answer that question?
kind regards,
Jos
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
JosAH
Suppose you have two numbers 'min' and 'max'; they contain the smallest and largest values of all numbers read so far.
i think i just dont understand how to do this step because i never learned it. i do understand the need for the updates after. can you tell me how?
Re: smallest number and largest number using while and if statements
Just forget about programming for a minute.
Imagine you are the "program".
You are told that in the end of the task you will need to tell what was the lowest and the highest number.
Someone will hand you over X numbers, 1 by 1.
Now how are you going to do this?
Remember all the numbers (maybe hundreds) so in the end you can look which was the highest and which the lowest?
What steps do you need to do?
Answer this in real life and you can "translate" it into software.
Re: smallest number and largest number using while and if statements
yes i think i understand what to do but i dont know how to do it. i know i have to set a max and a min and this is where the first 2 numbers will go. then using the if statements i have to keep updating it.
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
vicu1
yes i think i understand what to do but i dont know how to do it. i know i have to set a max and a min and this is where the first 2 numbers will go. then using the if statements i have to keep updating it.
Don't even bother about the first two numbers; there is one special situation: you don't have any numbers yet. That notion begs for a boolean value; part of the code might look like this:
Code:
if (noNumbersYet) min= currentNumber;
else if (currentNumber < min) min= currentNumber;
noNumbersYet= false;
You have to initialize that boolean value to true before you start receiving the numbers, that's all and you don't have to initialize that 'min' or 'max' value to some artificial value ...
kind regards,
Jos
Re: smallest number and largest number using while and if statements
you can use this algo if you want to-
int lar=-99999,small=999999,i=0;
String s;
int num=0;
while(i<count){//count=no. of numerals entered
System.out.println("Enter the number:");
try{
s=br.readLine();
num=Integer.parseInt(s);
}catch(IOException ioe){
System.out.println(ioe);
}
if(num>lar){
lar=num;
}
if(num<small){
small=num;
}
i++;
}
System.out.println("Largest:"+lar+"\nSmallest:"+sm all);
Re: smallest number and largest number using while and if statements
Cool, now suppose the largest number in the sequence of numbers is < -99999 or the smallest number > 99999 ...
kind regards,
Jos
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
JosAH
Cool, now suppose the largest number in the sequence of numbers is < -99999 or the smallest number > 99999 ...
kind regards,
Jos
algorithm constraints......... :P
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
Cosmos
algorithm constraints......... :P
Nope, those are arbitrary constraints and arbitrary constraints are just silly.
kind regards,
Jos
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
Cosmos
algorithm constraints......... :P
No, edge cases which are actually quite far from the edge. Wouldn't it be better to use say the constants from the Integer or Long class?
Re: smallest number and largest number using while and if statements
but i think its a better algorithm then storing all the elements of array and then searching.....i think we can set the variables as double and assign them value of infinity....can we????
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
Cosmos
but i think its a better algorithm then storing all the elements of array and then searching.....i think we can set the variables as double and assign them value of infinity....can we????
Who said anything about using an array here? And no, don't change the problem by making the numbers double. And no, don't spoon feed answers to the original poster, even if they are incorrect.
Re: smallest number and largest number using while and if statements
it actually worked.....just change the line containing
int lar=-99999,small=99999 with
double lar=-1/0.,small=1/0.;
its working fine.....
Re: smallest number and largest number using while and if statements
Code:
double lar=-1/0.,small=1/0.;
Don't you think it's slightly odd to be casting and comparing the values as doubles when they're just ints? And what would you do if long values were to be allowed in the sequence? (there are more longs than doubles, so some comparisons must give odd answers)
[Edit] Jos and Fubarable, above, have given standard answers to what is a standard problem. I don't think there's any value to the less flexible approach of dividing by zero.
Re: smallest number and largest number using while and if statements
ok i understand what you are saying josh but what would the noNumbersYet variable be?
Re: smallest number and largest number using while and if statements
Quote:
Originally Posted by
vicu1
ok i understand what you are saying josh but what would the noNumbersYet variable be?
It's a simple boolean variable, initialized to true, because at the start there were no numbers read yet.
kind regards,
Jos