#include <cstdlib>
#include <iostream>
class NumberPlayer {
int totalNumber;
int* numbers;
public:
NumberPlayer(int);
~NumberPlayer();
void setNumber(int, int);
int getNumberAt(int);
double getAverage();
int getLargest();
int getSmallest();
int getGreaterThanAverage();
};
NumberPlayer::NumberPlayer (int tn) {
totalNumber = tn;
numbers = new int[totalNumber];
}
NumberPlayer::~NumberPlayer () {
delete numbers;
}
void NumberPlayer::setNumber(int idx, int val) {
if (0 > idx || idx > totalNumber -1) {
std::cerr << "Error: Index number (" << idx << ") is wrong!" << std::endl;
exit(1);
}
if (!numbers) {
std::cerr << "Error: Player is not ready!" << std::endl;
exit(1);
}
numbers[idx] = val;
}
int NumberPlayer::getNumberAt(int idx) {
if (0 > idx || idx > totalNumber -1) {
std::cerr << "Error: Index number (" << idx << ") is wrong!" << std::endl;
exit(1);
}
if (!numbers) {
std::cerr << "Error: Player is not ready!" << std::endl;
exit(1);
}
return numbers[idx];
}
double NumberPlayer::getAverage () {
int i, total = 0;
for (i = 0; i < totalNumber; ++i) {
total += numbers[i];
}
return total/totalNumber;
}
int NumberPlayer::getLargest () {
int i, largest = numbers[0];
for (i = 1; i < totalNumber; ++i) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
return largest;
}
int NumberPlayer::getSmallest () {
int i, smallest = numbers[0];
for (i = 1; i < totalNumber; ++i) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
return smallest;
}
int NumberPlayer::getGreaterThanAverage () {
int i, totalNumberGreaterThanAverage = 0;
double average = getAverage();
for (i = 0; i < totalNumber; ++i) {
if (numbers[i] > average) {
++totalNumberGreaterThanAverage;
}
}
return totalNumberGreaterThanAverage;
}
int main() {
int i, input, tn = 0;
std::cout << "How many numbers do you want to enter? ";
std::cin >> tn;
if (tn < 1) {
std::cout << "Number is too small!" << std::endl;
return 1;
}
NumberPlayer player(tn);
for (i = 0; i < tn; ++i) {
std::cout << "Enter number: ";
std::cin >> input;
player.setNumber(i, input);
}
std::cout << "You have entered: ";
for (i = 0; i < tn; ++i) {
std::cout << player.getNumberAt(i);
if (i != tn -1) {
std::cout << ", ";
}
}
std::cout << "The average of the series numbers is: " << player.getAverage() << std::endl;
std::cout << "The largest of the series numbers is: " << player.getLargest() << std::endl;
std::cout << "The smallest of the series numbers is: " << player.getSmallest() << std::endl;
std::cout << "The total number greater than average is: " << player.getGreaterThanAverage() << std::endl;
return 0;
}