# Stanford CS106A Assignment 2 Find Range Solution

The Find Range solution requires some serious logic. Here is the problem:

Here is my solution, also available on gist:

The hardest part for me was actually figuring out how to use the while loop outside of Karel’s world, where all the while loops started with “while (frontIsClear)” and where they ended with there being a wall in front of Karel. Here, the while loop needs to run as long as the number the user provides is not equivalent to the sentinal, so you have to stop the loop by assigning the sentinal to the first number.

```&lt;pre&gt;/*
* File: FindRange.java
* Name:
* --------------------
* This file is the starter file for the FindRange problem.
*/

import acm.program.*;

public class FindRange extends ConsoleProgram {

private static final int sentinal = 0;

public void run() {
displayWelcomeMessage();
findRange();
}
private void displayWelcomeMessage() {
println(&quot;This program finds the largest and smallest numbers&quot;);
}
private void findRange() {
if (firstNumber == sentinal) {
println(&quot;this is not a valid first value&quot;);
} //if the first number is the sentinal, displays message
int smallestNumber = firstNumber; //the first number is currently the smallest number
int largestNumber = firstNumber; //the first number is also the largest number

/*Pre-condition: the first number does not equal to the sentinal.
* compares each new number the user enters to the existing smallest and largest numbers,
* and stores them as the smallest or largest if they are smallest or largest
*/

while ( firstNumber != sentinal) {
int secondNumber = readInt (&quot;?&quot;); //requests user to input the next number
if (secondNumber &amp;lt; smallestNumber) { //compares the new number to the smallest number
if (secondNumber != sentinal) {
smallestNumber = secondNumber;
}
// if the new number is the smaller than the smallest number,
//it is now stored as the smallest number
}
if (secondNumber &amp;gt; largestNumber) { //compares the new number to the largest number
if (secondNumber != sentinal) {
largestNumber = secondNumber;
}
//if the new number is larger than the largets number,
//it is now stored as the largest number
}
if(secondNumber == sentinal) { //if the new number is equal to the sentinal
println (&quot;smallest: &quot; + smallestNumber); //prints out smallest number
println (&quot;largest: &quot; + largestNumber); //pringt out largest number
firstNumber = sentinal; //assigns &quot;sentinal&quot; to the first number to stop the while loop
}

}
}
}&lt;/pre&gt;

```

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.

• Hi Natasha,
Its really good to share your coding experience.
You can also use this tag

``` to put your code while posting so it will be highlighted better and make user to read better. Because wordpresss automatically does this alignment and highlighting

[sourcecode language="css"]

```

Nirmal

• Thanks! This is extremely helpful!

• Hi, here is my solution

```import acm.program.*;

public class FindRange extends ConsoleProgram {

public void run(){
int nLarge = 0;
int nSmall = 0;

println("this program finds the largest and smallest numbers.");
int x = readInt(" ? ");
if (x == SENTINEL){
println("No values entered");

}else{
nLarge = x;
nSmall = x;

while (x!= SENTINEL){
if(x > nLarge){ nLarge = x;
}else if ( x < nSmall && x != 0){
nSmall = x;
}
}
println("Smallest " + nSmall);
println("Largest " + nLarge);

}
}

/*PRIVATE CONSTANTS*/
private static final int SENTINEL = 0;
}
```
• ```import acm.program.*;

public class FindRange extends ConsoleProgram {
//private constant
private static final int SENTINEL=0;

public void run(){
println("This program finds the largest and smallest numbers");

//asks the user for a number
//checks to see if the first number is 0 if it is see COMMENT#1
findTheRange();
} else {
//COMMENT#1 tells the user that there are no values if the first number he gave
//was zero
println("There are no values");
restartFindRange();
}
//Quits the program
System.exit(0);

}
/*
* This is where all the action happens
* The largest and lowest number is set to the number given by the user
*/
private void findTheRange(){
/*
* if this is the first time the program runs the find range method then
* the readNo is read from the user and is stored to both largest and smallest
* if the user has entered the SENTINEL once and typed "yes" to restart the process,
* then the largest and lowest number is set to 0 b
*/
while (true){
/*
* I use this if statement because if the user decides to restart the
* program when he has successfully finished finding the max and min
* the first time then the largest will be calculated and stored but the
* smallest will stay at zero as we set a private instance of zero to it
* (i hope this make sense. If you dont understand just remove this if statement
* and run the program. It will work successfully the first time but once you reach
* the end of the program and type "yes" without the quotes to restart it you will see
* that minimum will stay zero)
*/
if(smallest==SENTINEL){
/* I set the largest to the smallest here for the second time
* around. The smallest must have the same number of the largest in case the
* user decides to give the program one number and then the SENTINEL then this is where
* I reset the smallest number
*/
smallest=largest;

}
/*
* the program asks for numbers until the user types in 0
*/
/*
* if the number given is larger than the previous number stored, or the initial 0
* then the new number will be stored as the largest one
*/

} else {
/*
* if it isnt then it checks two things if it is smaller than the previous number given
* and it must not be zero, if the statement is true then the number given is set to the
* smallest
*/
} else{
/*
* if the number given is zero then the user is notified that the process has ended and
* shows him the smallest and largest numbers
*
*
*/
println("You have terminated the program. The smallest number is "+smallest+" and the largest number is "+largest);
/*the method restartFindRange is then called
*
*/
restartFindRange();
/*if the user does not decide to restart the program then the method stops here
*
*/
break;
}

}
}
}

}

/*
* this method allows the user to restart the process
*/
private void restartFindRange(){
println("Do you want to try again? ");
String restart=readLine("Type yes to restart or any other button to quit: ");
if (restart.contains("yes")){
findTheRange();
}
}

//private instance variables
private int largest=0;

private int smallest=0;

}

```
• Krys

Here is my solution: http://pastebin.com/H6vePpGN

• Nathan

Here’s mine ðŸ™‚

```import acm.program.*;
import java.awt.*;

public class DefaultConsole extends ConsoleProgram{
public static int SENTINEL=0;
public void run()
{
int a=-1, low, high=0, count=0, last;

println(&quot;This program finds the smallest and largest numbers.&quot;);

do{
last = a;
if (a!=0)
low = (a &lt; low) ? a: low;  // ternary operators ðŸ™‚
high = (a &gt; high) ? a: high;
count++;

}while(a !=SENTINEL);

if (count==1) {
println(&quot;No numbers entered...&quot;); }
else {
if (count==2)
low=high=last;
println(&quot;Highest is &quot; + high);
println(&quot;Lowest is &quot; + low);
}

}

}```
• Guest

here is my solution

import acm.program.*;

public class FindRange extends ConsoleProgram {

private static final int SENTINEL = 0;

public void run() {

int last=0, min=0, max=0; //defining the 3 integers

while (true){ /* 1st loop breaking in case of sentinel reached and defining min & /max
*/
if (last == SENTINEL) println(“merde quoi…”);
if (last == SENTINEL) break;
min = last;
max = last;

while (true) { /* 2nd loop defines min if last, and breaks if /sentinel
*/

``` import acm.program.*;```
``` public class FindRange extends ConsoleProgram { private static final int SENTINEL = 0; public void run() { int last=0, min=0, max=0; //defining the 3 integers while (true){ /* 1st loop breaking in case of sentinel reached and defining min & /max */ last = readInt(":"); if (last == SENTINEL) println("merde quoi..."); if (last == SENTINEL) break; min = last; max = last; while (true) { /* 2nd loop defines min if last, and breaks if /sentinel */ last = readInt(":"); if (last == SENTINEL) break; if(lastmax) max = last; } println("smallest:" + min + ""); println("biggest:" + max + "");} } ```