Results 1 to 5 of 5

Thread: loop in a half

  1. #1
    Brain_Child is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default loop in a half

    I wrote this program, it accepts values from the user until the sentinel value of 0 is reached, it then prints the highest and lowest numbers entered. (it is meant to print a message if no values were entered)

    Java Code:
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    	
    	private static final int SENTINEL = 0;
    	
    	public void run() {
    
           println("This program finds the largest and smallest numbers.");
           int largest;
           int smallest;
           int value = readInt("?");
           if (value == SENTINEL) {
        	   println("No values were entered");
           } else {
        	   smallest = value;
        	   largest = value;
        	   while (value!=SENTINEL) {
        	   if (value < smallest) {
        		   smallest = value;
        	   } else if (value > largest) {
        		   largest = value;
        	   }
        	   
        	   value = readInt("?");
        	   }
        	   
        	   println("smallest: " + smallest);
        	   println("largest: " + largest);
           }
        	   
    		
    	}
    }
    that program works fine, but in the next lecture, this idea of a loop and a half was introduced where a program of the form

    Java Code:
        read value
        while (value != sentinel)
            process value
            read value
    can be rewritten as

    Java Code:
        while (true)
            read value
            if (value == sentinel) break;
            process value
    to avoid code duplication. to me it looks like this question was designed to be solved using the loop and a half method, but i cant seem to crack it. i tried this code

    Java Code:
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    	
    	private static final int SENTINEL = 0;
    	
    	public void run() {
    
    	   println("This program finds the largest and smallest numbers.");
    	   int smallest = 0;
    	   int largest = 0;
    	   int value = 0;
    	   
    	   while (true) {
    		   value = readInt("?");
    		   
    		   if (value == SENTINEL) {
    	    	   println("smallest: " + smallest);
    	    	   println("largest: " + largest);
    	    	   break;
    		   }
    		   if (value > largest) {
    			   largest = value;
    		   } else if (value < smallest) {
    			   smallest = value;
    		   }
    		   
    	   }
    	}
    }
    but its has a major flaw in that the smallest value must be < 0 and the greatest value must be < 0. and i cant really see an easy way to solve this problem. (all i can think of is that the first value entered must be stored as both the smallest and largest value, but this must occur outside of the while loop, which just brings about a mess. or i could use a counter, but thats another variable, again a mess.)

    to me the idea of the loop and a half thing is to simplify code, but it looks like i will have to make the code more complex in order to implement the loop and a half.

    firstly, is it possible to simplify my original program using the loop and a half technique?

    secondly, can anyone help me? some hints perhaps?

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

    Default

    Have a look at the Integer class; it has two special constant values: the smallest and largest possible int value. Use them for your start values of your largest and smallest variables respectively.

    kind regards,

    Jos

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    another trick you could do with the loop control condition, is to use the "result of an assignment operation" can be used to test for a value.

    for example,

    Java Code:
    int value;
    while ( (value = readInt("?") )  !=  SENTINAL) {
     // the body
    }
    where we invoke readInt, and store into value, the result of asignment will be the value itself, we wrap that with the parenthesis, and evaluate it as != SENTINAL there to drive the while loop.

    And I don't think the test against largest, and smallest should be an if.. else thing. these are kind of independent quantities aren't they. ?

    the entire code becomes:
    Java Code:
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    	
    	private static final int SENTINEL = 0;
    	
    	public void run() {
    
    	   println("This program finds the largest and smallest numbers.");
    	   int smallest = 0;
    	   int largest = 0;
    	   int value = 0;
    	   
    	   while ( (value = readInt("?") ) != SENTINEL ) {
    
    		   if (value > largest) {
    			   largest = value;
                       }
                     
    		   if (value < smallest) {
    			   smallest = value;
    		   }		   
    	   } // while
    
    	   println("smallest: " + smallest);
    	   println("largest: " + largest);
    	    	 	  
    	} // run()
    }

  4. #4
    Brain_Child is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    excellent, thanks guy

  5. #5
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Alternative solution with only one read...

    Hey, what do you thinks of this?
    The basic problem to solve is the initialization of the variables, which is solved with an extra verification only needed for one of them because it is supposed that both are initialized at the same time.
    At the begining the variables should be initialized with the sentinel, after, inside the while loop, just check if the they are still equal to the sentinel, if so initialize then to the value read.

    Hope this helps.

Similar Threads

  1. Replies: 11
    Last Post: 10-27-2009, 07:55 AM
  2. Replies: 7
    Last Post: 01-30-2009, 06:40 AM
  3. Telecommute only at half rate
    By Johnny Kewl in forum Jobs Wanted
    Replies: 0
    Last Post: 05-11-2008, 04:05 PM
  4. Half a semester into java
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 10:05 AM
  5. JAVA Architect Position - Robert Half Technology
    By pegitha in forum Jobs Offered
    Replies: 0
    Last Post: 05-18-2007, 08:40 PM

Posting Permissions

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