fork download
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. class NumberPlayer {
  5. int totalNumber;
  6. int* numbers;
  7. public:
  8. NumberPlayer(int);
  9. ~NumberPlayer();
  10. void setNumber(int, int);
  11. int getNumberAt(int);
  12. double getAverage();
  13. int getLargest();
  14. int getSmallest();
  15. int getGreaterThanAverage();
  16. };
  17.  
  18. NumberPlayer::NumberPlayer (int tn) {
  19. totalNumber = tn;
  20. numbers = new int[totalNumber];
  21. }
  22.  
  23. NumberPlayer::~NumberPlayer () {
  24. delete numbers;
  25. }
  26.  
  27. void NumberPlayer::setNumber(int idx, int val) {
  28. if (0 > idx || idx > totalNumber -1) {
  29. std::cerr << "Error: Index number (" << idx << ") is wrong!" << std::endl;
  30. exit(1);
  31. }
  32. if (!numbers) {
  33. std::cerr << "Error: Player is not ready!" << std::endl;
  34. exit(1);
  35. }
  36. numbers[idx] = val;
  37. }
  38.  
  39. int NumberPlayer::getNumberAt(int idx) {
  40. if (0 > idx || idx > totalNumber -1) {
  41. std::cerr << "Error: Index number (" << idx << ") is wrong!" << std::endl;
  42. exit(1);
  43. }
  44. if (!numbers) {
  45. std::cerr << "Error: Player is not ready!" << std::endl;
  46. exit(1);
  47. }
  48. return numbers[idx];
  49. }
  50.  
  51. double NumberPlayer::getAverage () {
  52. int i, total = 0;
  53. for (i = 0; i < totalNumber; ++i) {
  54. total += numbers[i];
  55. }
  56. return total/totalNumber;
  57. }
  58.  
  59. int NumberPlayer::getLargest () {
  60. int i, largest = numbers[0];
  61. for (i = 1; i < totalNumber; ++i) {
  62. if (numbers[i] > largest) {
  63. largest = numbers[i];
  64. }
  65. }
  66. return largest;
  67. }
  68.  
  69. int NumberPlayer::getSmallest () {
  70. int i, smallest = numbers[0];
  71. for (i = 1; i < totalNumber; ++i) {
  72. if (numbers[i] < smallest) {
  73. smallest = numbers[i];
  74. }
  75. }
  76. return smallest;
  77. }
  78.  
  79. int NumberPlayer::getGreaterThanAverage () {
  80. int i, totalNumberGreaterThanAverage = 0;
  81. double average = getAverage();
  82. for (i = 0; i < totalNumber; ++i) {
  83. if (numbers[i] > average) {
  84. ++totalNumberGreaterThanAverage;
  85. }
  86. }
  87. return totalNumberGreaterThanAverage;
  88. }
  89.  
  90. int main() {
  91.  
  92. int i, input, tn = 0;
  93.  
  94. std::cout << "How many numbers do you want to enter? ";
  95. std::cin >> tn;
  96.  
  97. if (tn < 1) {
  98. std::cout << "Number is too small!" << std::endl;
  99. return 1;
  100. }
  101.  
  102. NumberPlayer player(tn);
  103.  
  104. for (i = 0; i < tn; ++i) {
  105. std::cout << "Enter number: ";
  106. std::cin >> input;
  107. player.setNumber(i, input);
  108. }
  109.  
  110. std::cout << "You have entered: ";
  111. for (i = 0; i < tn; ++i) {
  112. std::cout << player.getNumberAt(i);
  113. if (i != tn -1) {
  114. std::cout << ", ";
  115. }
  116. }
  117.  
  118. std::cout << "The average of the series numbers is: " << player.getAverage() << std::endl;
  119. std::cout << "The largest of the series numbers is: " << player.getLargest() << std::endl;
  120. std::cout << "The smallest of the series numbers is: " << player.getSmallest() << std::endl;
  121. std::cout << "The total number greater than average is: " << player.getGreaterThanAverage() << std::endl;
  122.  
  123. return 0;
  124. }
  125.  
Success #stdin #stdout 0s 3436KB
stdin
4
4
2
8
6
stdout
How many numbers do you want to enter? Enter number: Enter number: Enter number: Enter number: You have entered: 4, 2, 8, 6The average of the series numbers is: 5
The largest of the series numbers is: 8
The smallest of the series numbers is: 2
The total number greater than average is: 2