Source of max2a.cpp


  1: //max2a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: using namespace std;

  8: /**
  9: Tests if one integer has a smaller digit sum than another.
 10: Pre:\n i1 and i2 have been initialized and i1, i2 are both > 0.
 11: Post:\n Returns true if sum of the digits in i1 is < sum of digits
 12: in i2, and otherwise returns false.
 13: */
 14: bool hasSmallerDigitSum
 15: (
 16:     int i1, //in
 17:     int i2  //in
 18: )
 19: {
 20:     int digitSum1 = 0;
 21:     while (i1 != 0)
 22:     {
 23:         digitSum1 += i1 % 10;
 24:         i1 /= 10;
 25:     }

 27:     int digitSum2 = 0;
 28:     while (i2 != 0)
 29:     {
 30:         digitSum2 += i2 % 10;
 31:         i2 /= 10;
 32:     }
 33:     return digitSum1 < digitSum2;
 34: }

 36: int main()
 37: {
 38:     cout << "\nThis program illustrates the use of the STL max() "
 39:             "algorithm (extended version)\nto find the maximum of two "
 40:             "integer literal values, the maximum of two integer\nvalues "
 41:             "stored in simple integer variables, and (in two different "
 42:             "ways) the\nmaximum of two integer values stored in a vector "
 43:             "of integers. In this case,\none integer is larger than "
 44:             "another if and only if it has a greater digit sum.";
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 47:     cout << "\nFirst, we find the maximum of two integer "
 48:         "literal values.\n";
 49:     cout << "max(16, 23) = " << max(16, 23, hasSmallerDigitSum);
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 52:     int first = 7;
 53:     int second = 10;
 54:     cout << "\nNext, we find the maximum of two integer "
 55:         "\nvalues stored in simple integer variables.";
 56:     cout << "\nfirst = " << first << "    second = " << second << endl;
 57:     cout << "max(first, second) = "
 58:         << max(first, second, hasSmallerDigitSum);
 59:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 61:     int a[] = {39, 66, 110, 81, 47};
 62:     vector<int> v(a, a+5);

 64:     cout << "\nHere are the integer values in the vector:\n";
 65:     for (vector<int>::size_type i=0; i<v.size(); i++)
 66:         cout << v.at(i) << " ";
 67:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 69:     cout << "\nNow we find the maximum of the first and last values "
 70:         "\nstored in the vector of integers using one approach.";
 71:     cout << "\nmax(v.at(0), v.at(4)) = "
 72:         << max(v.at(0), v.at(4), hasSmallerDigitSum);
 73:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 75:     cout << "\nFinally, we find the maximum of the first and last values "
 76:         "\nstored in the vector of integers using a second approach.";
 77:     cout << "\nmax(*v.begin(), *(v.end()-1)) = "
 78:         << max(*v.begin(), *(v.end()-1), hasSmallerDigitSum);
 79:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 80: }