Wednesday, February 24, 2010

FindLargest Problem

Problem: "Write a ConsoleProgram that reads in a list of integers, one per line, until the users a sentinel value of 0. When the sentinel is read, your program should display the largest value in the list" (Robers ch4, problem 12).

Renee's Code:

/*
* File: FindLargest.java
* Name: Renee
* Section Leader: Me! I did this one before breakfast, =D
* ------------------
We use two variables, "value" and "top", to solve this problem. "Top" is
declared at the beginning of the run method and arbitrarily gets assigned
-1. "Value" comes from the user's imput. Every time the user enters
Value comes the user's input. For every input, we check the input value against
the current "top," and if it's greater, we replace "top" with the "value."
Then once the user enters the sentinel, we print the current "top."
*/

import acm.program.*;

public class FindLargest extends ConsoleProgram{
public void run () {
println ("This program finds the largest of a list of integers.");
println("Enter integers, one per line, entering 0 to signify the end of the list.");

int top = -1;

while (true){
int value = readInt("Enter integer: ");
if (value == SENTINEL) break;

if (value >= top) top = value;

}

println("The highest is " + top + ".");

}
//private constants
private static final int SENTINEL = 0;


}
What it looks like:


How long it took: 25 min

Lingering Questions: I solved this by using two variables ("Top" and "Value") whose value changed, as the comment for the code describes. The question is: What are alternative approaches to solving this?

4 comments:

  1. hmm, what if the user only enters 0? then we get an answer, "the highest is -1"; crappers.

    ReplyDelete
  2. Instead of initializing top with -1, you can initialize with the first input. That way your program would work for negative integers too. That would also remove the "highest is -1" answer.

    ReplyDelete
  3. Hmm. Do you mean this?

    import acm.program.*;

    int top = readint;

    while (true){
    int value = readInt("Enter integer: ");
    if (value == SENTINEL) break;

    if (value >= top) top = value;

    }

    println("The highest is " + top + ".");

    }
    //private constants
    private static final int SENTINEL = 0;


    }

    If we start with "int top = readint", then what if the person enters 0 first, and then they enter another number and keep going? That's a problem because they've entered the sentinel, but the program didn't stop like it should! (Let me know if this isn't what you meant, shridhar :)

    I think the right way to do it is to initialize "top" as zero. I had been wary of using the sentinel as the initial value, but that's silly because the value is set OUTSIDE the while loop, the the point of the sentinel is only to break us out of the while loop.

    So, here is my revised code; i think it is bug-free:

    import acm.program.*;

    public class FindLargest extends ConsoleProgram{
    public void run () {
    println ("This program finds the largest of a list of integers.");
    println("Enter integers, one per line, entering 0 to signify the end of the list.");

    int top = 0;

    while (true){
    int value = readInt("Enter integer: ");
    if (value == SENTINEL) break;

    if (value >= top) top = value;

    }

    println("The highest is " + top + ".");

    }
    //private constants
    private static final int SENTINEL = 0;


    }

    ReplyDelete
  4. I was thinking more of something like this:

    ...
    int value = readInt("Enter integer:");
    int top = value;
    while (value!=SENTINEL){
    value = readInt("Enter integer:");
    if (value>top) top = value;
    }
    ...

    ReplyDelete