Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default 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:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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... ;(

  4. #4
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by vicu1 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by JosAH View Post
    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?

  7. #7
    AlphaSupport Guest

    Default 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.

  8. #8
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by vicu1 View Post
    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:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default 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);

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by JosAH View Post
    Cool, now suppose the largest number in the sequence of numbers is < -99999 or the smallest number > 99999 ...

    kind regards,

    Jos
    algorithm constraints......... :P

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by Cosmos View Post
    algorithm constraints......... :P
    Nope, those are arbitrary constraints and arbitrary constraints are just silly.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by Cosmos View Post
    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?

  15. #15
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default 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????

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by Cosmos View Post
    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.

  17. #17
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default 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.....

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: smallest number and largest number using while and if statements

    Java 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.

  19. #19
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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?

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by vicu1 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Find the Biggest Number + Smallest
    By d4v1dz in forum New To Java
    Replies: 19
    Last Post: 10-26-2011, 07:43 AM
  2. How can I find out the largest number ... ?
    By TheTypicalStudent in forum New To Java
    Replies: 14
    Last Post: 05-03-2011, 12:09 PM
  3. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 06:08 PM
  4. smallest number java program
    By elmo in forum New To Java
    Replies: 9
    Last Post: 11-18-2010, 10:29 PM
  5. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •