Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: loop and a half

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default loop and 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)

    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

        read value
        while (value != sentinel)
            process value
            read value

    can be rewritten as

        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

    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
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Thumbs up Re: loop and a half

    Shouldn't you seed your largest and smallest like this?

    int largest = Integer.MIN_VALUE;
    int smallest = Integer.MAX_VALUE;
    Then after reading the first value you set them both to the input as long as the first value isn't 0.

    This is how I would have solved the problem the first time....

     
            static final int SENTINEL = 0;
    	int largest = Integer.MIN_VALUE;
    	int smallest = Integer.MAX_VALUE;
     
           value = Console.readInt();
    	 while(value != SENTINEL)
    	 {
    		 if(value>largest)
    			 largest = value;
    		 if(value<smallest)
    			 smallest = value;
    		 value = Console.readInt();
    	 }
     
    	 System.out.println("Smallest value = "+smallest);
    	 System.out.println("Largest Value = "+largest);

    Now, if I understand the "loop and a half" you are asking for a do-while like this...

    		do
    		{
    			 value = Console.readInt();
                             if(value !=SENTINEL)
                            {
    			    if(value>largest)
    				 largest = value;
    			    if(value<smallest)
    				 smallest = value;
                            }
    		}
    		while(value != SENTINEL);
    	 System.out.println("Smallest value = "+smallest);
    	 System.out.println("Largest Value = "+largest);

    I think this is what you are looking for - hope this helps!

  3. #3
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: loop and a half

    There is still a problem with the solution provided by mdv2000, which is that if the first value entered is SENTINEL, smallest and largest remain as Integer.MAX_VALUE and Integer.MIN_VALUE respectively. But I think it's okay if we remove a single if statement:

    	static final int SENTINEL = 0;
    	int largest = Integer.MIN_VALUE;
    	int smallest = Integer.MAX_VALUE;
     
    	do
    	{
    		value = Console.readInt();
     
    		if(value>largest) largest = value;
    		if(value<smallest) smallest = value;
    	}
    	while(value != SENTINEL);
     
    	System.out.println("Smallest value = "+smallest);
    	System.out.println("Largest Value = "+largest);

Similar Threads

  1. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  2. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  3. literal Loop
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 10th, 2009, 10:03 PM
  4. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM