fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /************************************************************
  5.  * Question 7 – C++ Class Extension
  6.  ************************************************************/
  7.  
  8. class NumberSet
  9. {
  10. private:
  11. int values[5];
  12.  
  13. public:
  14. // Constructor
  15. NumberSet(int a, int b, int c, int d, int e)
  16. {
  17. values[0] = a;
  18. values[1] = b;
  19. values[2] = c;
  20. values[3] = d;
  21. values[4] = e;
  22. }
  23.  
  24. // Returns the largest value
  25. int Largest() const
  26. {
  27. int max = values[0];
  28. for (int i = 1; i < 5; i++)
  29. {
  30. if (values[i] > max)
  31. max = values[i];
  32. }
  33. return max;
  34. }
  35.  
  36. // Returns the smallest value
  37. int Smallest() const
  38. {
  39. int min = values[0];
  40. for (int i = 1; i < 5; i++)
  41. {
  42. if (values[i] < min)
  43. min = values[i];
  44. }
  45. return min;
  46. }
  47.  
  48. // Returns the sum of all values
  49. int Total() const
  50. {
  51. int sum = 0;
  52. for (int i = 0; i < 5; i++)
  53. sum += values[i];
  54.  
  55. return sum;
  56. }
  57.  
  58. // Returns the average of the values
  59. double Average() const
  60. {
  61. return static_cast<double>(Total()) / 5.0;
  62. }
  63. };
  64.  
  65. /************************************************************
  66.  * main – demonstration only
  67.  ************************************************************/
  68.  
  69. int main()
  70. {
  71. NumberSet nums(10, 5, 25, 3, 12);
  72.  
  73. cout << "Largest: " << nums.Largest() << endl;
  74. cout << "Smallest: " << nums.Smallest() << endl;
  75. cout << "Total: " << nums.Total() << endl;
  76. cout << "Average: " << nums.Average() << endl;
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Largest: 25
Smallest: 3
Total: 55
Average: 11