user14665723
user14665723

Reputation:

What is the purpose of taking the largest number as an input when you are computing the largest value in a sequence?

int main ()
{
    double largest; cin >> largest;
    double input;
    while (cin >> input)
    {
        if (input > largest)
        {
            largest = input;
        }
    }
}

Why are we taking largest as a user input when clearly that is what we want out of this code fragment?

Upvotes: 0

Views: 71

Answers (3)

ShadowRanger
ShadowRanger

Reputation: 155506

If you don't set largest to something, you're going to have problems; having an unset stack value means largest has a garbage value and you'll get undefined behavior when you test if (input > largest). So that's the worst option; you don't want nasal demons.

You could initialize it -HUGE_VAL, or -DBL_MAX if you must avoid infinities (-std::numeric_limits<double>::infinity() and -std::numeric_limits<double>::max() or std::numeric_limits<double>::lowest() if you like to use C++ includes to simplify; the C headers distribute the limits for different types across multiple headers), instead to get a hugely negative value that any input would be larger than, and the code would be mostly the same, with the main difference being what happens if the user fails to provide any input; with a constant initializer, largest would remain that value (it never gets reassigned), while with cin initialization, it would get whatever cin does when it fails to parse a value (IIRC, for C++11 and later, 0 gets stored, before C++11 it stores nothing and we're still in nasal demons land).

The primary reason to initialize it from cin is just for simplicity; when we've only taken one input, then that input is the largest so far. As we loop, largest will remain the largest so far as we update it on demand, and when the loop is over, the largest so far also happens to be the largest overall, and our result is already in an appropriately named variable.

Technically, initializing from cin is probably trivially faster (given it avoids an unnecessary comparison in the first loop where initializing to -HUGE_VAL requires that test), but that's almost certainly meaningless relative to all the other overhead involved (e.g. reading from a file handle and parsing it).

Upvotes: 3

user1196549
user1196549

Reputation:

The code transfers the input value to the variable largest, provided it is larger than the previously input values.

But this cannot work with the first value, as there was no previous. The fix is to assign the first value to largest unconditionally. (This is a correct measure, as the largest among a list of a single value is that value.)


A variant is to initialize largest with the smallest possible value.

int main ()
{
    double largest= std::numeric_limits<double>::lowest();
    double input;
    while (cin >> input)
    {
        if (input > largest)
        {
            largest = input;
        }
    }
}

There is a slight difference between the two versions: the first will require at least one value before it can exit. The second does not, and in case no value is input, it will return the smallest possible double.

Upvotes: 0

Tom Gebel
Tom Gebel

Reputation: 805

You have to add more information for this question to be answered appropriately, but in this little code snippet, largest is acting as a kind of starting point for comparison between user input and the by now largest input.

Upvotes: 0

Related Questions