Source of extreme_values.cpp


  1: //extreme_values.cpp
  2: //Finds the largest negative integer and smallest integer among the
  3: //input values. Illustrates a sentinel-controlled loop containing one
  4: //test in its body, and followed by two sequential tests.

  6: #include <iostream>
  7: #include <climits>
  8: using namespace std;

 10: int main()
 11: {
 12:     cout << "\nThis program determines the largest negative integer "
 13:         "and the smallest\npositive integer entered by the user. "
 14:         "Terminate input by entering 0.\n\n";

 16:     int largestNeg = INT_MIN;
 17:     int smallestPos = INT_MAX;
 18:     int newValue;

 20:     cout << "Enter your integer sequence starting on the line below:\n";
 21:     cin >> newValue;
 22:     while (newValue != 0)
 23:     {
 24:         if (newValue < 0  &&  newValue > largestNeg)
 25:             largestNeg = newValue;
 26:         else if (newValue > 0  &&  newValue < smallestPos)
 27:             smallestPos = newValue;
 28:         cin >> newValue;
 29:     }

 31:     if (largestNeg == INT_MIN)
 32:         cout << "Either no negative integers were input or the largest "
 33:             "was the value\n" << "of INT_MIN, i.e. " << INT_MIN << ".\n";
 34:     else
 35:         cout << "The largest negative integer was "
 36:              << largestNeg << ".\n";

 38:     if (smallestPos == INT_MAX)
 39:         cout << "Either no positive integers were input or the smallest "
 40:             "was the value\nof INT_MAX, i.e. " << INT_MAX << ".\n";
 41:     else
 42:         cout << "The smallest positive integer was "
 43:              << smallestPos << ".\n";
 44:     cout << endl;
 45: }