"The Greatest and Least of These"

I'm going through a school book from last semester. I am struggling with understanding why the following code is not working.

// Greatest and Least of These
// Allow the user to enter a series of integers with a loop
// The user will enter -99 to signal the end of the series
// AFTER all numbers have been entered, the program should display the largest and
// smallest numbers entered

#include <iostream>

using namespace std;

int main()
{
// variables
int num;
int min;
int max;


// allow user to enter a series of integers with a loop
do{
cout << "Enter a series of integers, -99 will stop your entry of the series\n";
cin >> num;

min = max = num;

if (num > max && num != -99)
max = num;
if (num < min && num != -99)
min = num;
} while (num != -99);

cout << "Largest: " << max << endl;
cout << "Smallest: " << min << endl;

return 0;
}


When I run the code and enter numbers, I am receiving -99 as both my highest and my lowest entry. Not sure at all where I am going wrong here. I would greatly appreciate any help. Thanks in advance.

You put min = max = num; inside your loop, immediately after your cin statement. Both min and max are getting set to num's value each time your loop runs. When the loop ends, num was -99, so both variables are too.

-Albatross
Okay, so I have gone back and tried to place this before the do while loop, after the do while loop, and after the if statements nested inside of the do while loop. I can not figure out where I am going wrong here.
There are ways to initialize min and max to the first value you set num to, but most of them involve changing your loop in some way.

You're probably best off setting them to their lowest and highest possible values, and omitting the min = max = num; line entirely.

-Albatross
Sorry, I am a bit confused at what you mean by setting them to their lowest and highest possible values. I understand how to do this, but the code is supposed to be wrote so that the code detects which value is the highest and lowest. It reads to me as if the max and the min will not have a number to initialize to, unless you mean 0. I have already tried to initialize all of the above to 0 and work it out. My code so far will read the highest value but still reads the lowest value as -99.
What I meant by setting them to their lowest and highest possible values was the following:

ints have limits on how large or small (i.e. how far away from zero) the values you store in them can be. In most cases these days, those limits are approximately positive and negative 2.1 billion. Computers where that isn't the case aren't rare, however, and it's better practice to check what the exact value is using numeric_limits: http://www.cplusplus.com/reference/limits/numeric_limits/#example

EDIT from the future: As per JLBorges's code below, you can also use INT_MAX and INT_MIN from the <climits> header, but it is a C feature with a C++ equivalent. Using such a feature will annoy some C++ programmers. Be advised.

If that looks a bit arcane to you and you'd really rather not do it, then here's a hint for something else you can initialize min and max to: there's one value which neither of them should be equal to when your program ends. If you decide to do that, note that you'll need to check for that value in your loop.

-Albatross

Problem for you to worry about only once you've solved your current problem: what should happen when the first number someone puts in is -99?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <climits>

int main()
{
    // The user will enter -99 to signal the end of the series
    // declare a constant, avoid magic numbers
    // https://www.oreilly.com/library/view/c-coding-standards/0321113586/ch18.html
    const int SENTINEL = -99 ;

    // INT_MAX, INT_MIN: https://en.cppreference.com/w/cpp/header/climits
    // initialise as recommended by the albatross
    // the idea is that every other int is smaller than INT_MAX and larger than INT_MIN
    int smallest = INT_MAX ; // initialise with the largest possible value of int
    int largest = INT_MIN ; // initialise with the smallest possible value of int
    int cnt = 0 ; // count of numbers entered by the user

    // Allow the user to enter a series of integers with a loop
    std::cout << "Enter a series of integers, " << SENTINEL
              << "will stop your entry of the series\n";

    // loop: read in numbers one by one till SENTINEL is entered
    // for each number entered by the user, increment cnt
    // we could use any loop construct here; this example uses a for loop
    //     initialisation: declare variable number
    //     condition: true if a number was read in AND the number is not the SENTINEL number
    //     increment: increment the count of valid numbers entered by the user
    for( int number ; std::cin >> number && number != SENTINEL ; ++cnt )
    {
        // check if the number entered by the user is the new smallest or largest
        // note that the first time through this loop:
        //           any number other than INT_MAX will be smaller than smallest
        //           any number other than INT_MIN will be larger than largest
        if( number < smallest ) smallest = number ;
        if( number > largest ) largest = number ;
    }

    // AFTER all numbers have been entered (once e come out of the loop),
    // the program should display the largest and smallest numbers entered

    if( cnt == 0 ) std::cout << "you did not enter any numbers\n" ; // sanity check

    else // at least one valid number was entered
    {
        std::cout << "you entered " << cnt << " numbers\n"
                  << " largest: " << largest << '\n'
                  << "smallest: " << smallest << '\n' ;
    }
}
Thank you. I have got my code running. I used the method JLBORGES provided to get a general understanding. I will continue to modify this code and removed the <climit> header and go about this another way.. but I have more programs to chug through. Thank you for all of your help
Another way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int main()
{
    const int SENTINEL = -99 ;
    std::cout << "Enter a series of integers, " << SENTINEL
              << " will stop your entry of the series\n";

    int number ;
    // read in the first number
    if( std::cin >> number && number != SENTINEL ) // if the user entered a valid first number
    {
        // initialise both smallest and largest to the first number that was read
        int smallest = number ;
        int largest = number ;

        // loop: read in the subsequent numbers one by one till SENTINEL is entered
        while( std::cin >> number && number != SENTINEL )
        {
            // check if the number entered by the user is the new smallest or largest
            if( number < smallest ) smallest = number ;
            if( number > largest ) largest = number ;
        }

        // display the largest and smallest numbers entered
        std::cout << " largest: " << largest << '\n'
                  << "smallest: " << smallest << '\n' ;
    }

    else std::cout << "you did not enter any numbers\n" ;
}
Topic archived. No new replies allowed.