Source of includes2a.cpp


  1: //includes2a.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 includes() "
 39:         "algorithm (extended\nversion) to test whether the values in a "
 40:         "second range of (ordered) integer\nvalues in a vector of "
 41:         "integers are all present in a first (ordered) range of\ninteger "
 42:         "values, also in a vector of integers. In this case the integers "
 43:         "are\nordered in the sense that a first integer precedes a second "
 44:         "if and only if\nits digit sum is less than the digit sum of the "
 45:         "second integer.";
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 48:     int a1[] = {11, 23, 15, 8, 55, 39, 96};
 49:     vector<int> v1(a1, a1+7);

 51:     cout << "\nHere are the contents of v1:\n";
 52:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 53:         cout << v1.at(i) << " ";
 54:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 56:     int a2[] = {11, 15, 39};
 57:     vector<int> v2(a2, a2+3);

 59:     cout << "\nHere are the contents of v2:\n";
 60:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 61:         cout << v2.at(i) << " ";
 62:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 64:     if (includes(v1.begin(), v1.end(), v2.begin(), v2.end(),
 65:         hasSmallerDigitSum))
 66:         cout << "\nv1 includes v2.";
 67:     else
 68:         cout << "\nv1 does not include v2.";
 69:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 71:     int a3[] = {14, 44, 66};
 72:     vector<int> v3(a3, a3+3);

 74:     cout << "\nHere are the contents of v3:\n";
 75:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 76:         cout << v3.at(i) << " ";
 77:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 79:     if (includes(v1.begin(), v1.end(), v3.begin(), v3.end(),
 80:         hasSmallerDigitSum))
 81:         cout << "\nv1 includes v3.";
 82:     else
 83:         cout << "\nv1 does not include v3.";
 84:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 86:     int a4[] = {14, 44, 65};
 87:     vector<int> v4(a4, a4+3);

 89:     cout << "\nHere are the contents of v4:\n";
 90:     for (vector<int>::size_type i=0; i<v4.size(); i++)
 91:         cout << v4.at(i) << " ";
 92:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 94:     if (includes(v1.begin(), v1.end(), v4.begin(), v4.end(),
 95:         hasSmallerDigitSum))
 96:         cout << "\nv1 includes v4.";
 97:     else
 98:         cout << "\nv1 does not include v4.";
 99:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
100: }