fork download
  1. //=========================================================
  2. // File: HW_10a
  3. // Programmer: Elaine Torrez
  4. // Class: CMPR 121
  5. //=========================================================
  6. // Description:
  7. // This program uses the max template function.
  8. // It returns the greater of two values.
  9. //=========================================================
  10.  
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <string>
  14.  
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19. int firstNumber;
  20. int secondNumber;
  21.  
  22. char firstLetter;
  23. char secondLetter;
  24.  
  25. double firstDecimal;
  26. double secondDecimal;
  27.  
  28. string firstWord;
  29. string secondWord;
  30.  
  31. firstNumber = 1;
  32. secondNumber = 2;
  33.  
  34. firstLetter = 'a';
  35. secondLetter = 'z';
  36.  
  37. firstDecimal = 3.14;
  38. secondDecimal = 2.72;
  39.  
  40. firstWord = "apple";
  41. secondWord = "zebra";
  42.  
  43. cout << "The greater value of "
  44. << firstNumber << " and "
  45. << secondNumber << " = "
  46. << max(firstNumber, secondNumber)
  47. << endl;
  48.  
  49. cout << "The greater value of "
  50. << secondNumber << " and "
  51. << firstNumber << " = "
  52. << max(secondNumber, firstNumber)
  53. << endl;
  54.  
  55. cout << "The greater value of '"
  56. << firstLetter << "' and '"
  57. << secondLetter << "' = "
  58. << max(firstLetter, secondLetter)
  59. << endl;
  60.  
  61. cout << "The greater value of "
  62. << firstDecimal << " and "
  63. << secondDecimal << " = "
  64. << max(firstDecimal, secondDecimal)
  65. << endl;
  66.  
  67. cout << endl;
  68.  
  69. cout << "Does the max function work with strings? Yes."
  70. << endl;
  71.  
  72. cout << "Strings can be compared alphabetically,"
  73. << " so max() works with string data types."
  74. << endl;
  75.  
  76. cout << "Example: "
  77. << max(firstWord, secondWord)
  78. << " is greater alphabetically."
  79. << endl;
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
The greater value of 1 and 2 = 2
The greater value of 2 and 1 = 2
The greater value of 'a' and 'z' = z
The greater value of 3.14 and 2.72 = 3.14

Does the max function work with strings? Yes.
Strings can be compared alphabetically, so max() works with string data types.
Example: zebra is greater alphabetically.