# Problem 5

The problem is “Determining the Range, Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list.

Your program should handle the following special cases:

• If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest.
• If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.

So once again broken down the problem into parts, first part need to read in the values from the keyboard and the next part is to set the lowest and the highest values (whilst making sure that no value takes the sentinel value).

Here is a example of the output from the running program

```This program finds the smallest and largest integers in a list. Enter values, one per line, using a 0 to signal the end of the list ?32 ?2 ?4 ?5 ?45 ?1 ?0 Smallest value was 1 Largest value was 45```

source code in full, I have included within zip file and also the PDF file of the full assignment problems.

```/* * File: FindRange.java * -------------------- * This program is a stub for the FindRange problem, which finds the * smallest and largest values in a list of integers. */   import acm.program.*; import java.io.Console;   public class FindRange extends ConsoleProgram {   public void run() { int readInValue=sential; println("This program finds the smallest and largest integers in a list. Enter values, one per line, using a "+sential+" to signal the end of the list"); do { readInValue = readInt("?"); if (readInValue != sential) { if (readInValue > largestValue) largestValue = readInValue; if (readInValue < smallestValue || smallestValue == sential) smallestValue = readInValue; } } while (readInValue != sential); if (largestValue == smallestValue && largestValue == sential) { println("No values inputted"); } else { println("Smallest value was "+ smallestValue); println("Largest value was " + largestValue); } }   private int largestValue = 0, smallestValue = 0; private static final int sential = 0; }```

## 4 thoughts on “Problem 5”

1. Subhadeep Gayen says:
```import acm.program.*;   public class FindRange extends ConsoleProgram { private static final int SENTINEL=0;   public void run() { // Initialisation of variables int smallNumber=0; int bigNumber=0; int x=0; int read;   // While Loop with break for SENTINEL while(true){ read=readInt("Enter number ");   // Make the 1st value same for both only once while(x==0){ smallNumber=read; bigNumber=read; x=1; }   // Conditions and Checks if(read==SENTINEL){break;} else if (read bigNumber){bigNumber=read;} } // Check for final Println if(bigNumber!=0 &amp;&amp; smallNumber!=0){ println("small number "+smallNumber+" big number "+bigNumber); }else{println("You have not entered any value");} } }```
2. Kenny says:

The sentinel should not be included in calculating the largest or smallest, it simply marks the end of the list.

However, if all the integers are negative, the program states that the largest integer is 0, which is wrong.

3. anon says:
```/* * File: FindRange.java * Name: * Section Leader: * -------------------- * This file is the starter file for the FindRange problem. */   import acm.program.*;   public class FindRange extends ConsoleProgram {   private static final int SENTINEL = 0;   public void run() {   //initializes variables int bigNumber = -2147483648; int smallNumber = 2147483647; int input; boolean firstZero = false;   println("This program find the smallest and largest integers in a list. Enter a 0 to signal the end of the list.");   while(true){ //takes the value the user enters and stores it in the variable input input = readInt("?");   //if the input is 0 and the initial variables have not changed, indicating that 0 was the first value entered, then we set the variable firstZero (which indicates the first value entered was zero) to true and break from the loop. if(input == SENTINEL &amp;&amp; bigNumber == -2147483648 &amp;&amp; smallNumber == 2147483647){ firstZero = true; break; }   //if the input is zero, break from the loop if(input == SENTINEL){break;}   //if the input is bigger than the value of bigNumber, then store it in bigNumber if(input &gt; bigNumber){ bigNumber = input; }   //if the input is smaller than the value of smallNumber, then store it in smallNumber //the first number entered will be stored in both variables, with two exceptions (-2147483648 &amp; 2147483647) if (input &lt; smallNumber){ smallNumber = input; } }   //if the first value entered was zero, display this text if(firstZero == true){ println(&quot;You have not entered any numbers.&quot;); //otherwise, display this text } else { println(&quot;small:&quot; + smallNumber + &quot;big:&quot; + bigNumber); } } }```

//note: this program works for ALL cases except for if the user ONLY enters 2147483647 and -2147483648. it will work if they enter one of them (alone or with other integers).

4. Vendetta says:
```/* * File: FindRange.java * Name: * Section Leader: * -------------------- * This file is the starter file for the FindRange problem. */   import acm.program.*;   public class FindRange extends ConsoleProgram { public void run() { println("This program finds the largest number"); println("Please type the number you want, one per line,using " + SENTINEL +".");   int value = readInt("? "); int biggest = value; int smallest = value; while (true) { int value2 = readInt("? "); if (value2 == SENTINEL) break; if (value2 > biggest) { biggest = value2; } if (value2 < smallest) { smallest = value2; } } println("Biggest:" + biggest +"."); println("Smallest:" + smallest +"."); } private static final int SENTINEL = 0; }```