#include #include #include #include #include #include using namespace std; int main () { unsigned seed = chrono::system_clock::now().time_since_epoch().count(); default_random_engine engine {seed}; uniform_int_distribution distribution {-100, 100}; auto r {bind(distribution, engine)}; cout << "How many random numbers would you like to generate? Please choose a number between 5 and 25.\n"; int quantity {0}; cin >> quantity; if (!cin) { cerr << "Sorry, invalid input.\n"; return EXIT_FAILURE; } else if (quantity < 5) { cerr << "Sorry, too low. Please enter a higher number.\n"; return EXIT_FAILURE; } else if (quantity > 25) { cerr << "Sorry, too high. Please enter a lower number.\n"; return EXIT_FAILURE; } else {}; int values[quantity]; for (int i = 0; i < quantity; ++i) { //Creates the array and assigns random values values[i] = r(); cout << values[i] << ", "; } cout << "Here is your array, sorted lowest to highest.\n"; for (int i = 0; i < quantity; ++i) { //Bubble sort function moves numbers to the right if they are higher than their neighbor for (int j = 0; j < quantity - i - 1; j++) { if (values[j] > values[j + 1]) { int temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } for (int i = 0; i < quantity; ++i) { cout << setw(4) << values[i] <<"\n"; } cout << "The average of this array is "; int sum = {0}; for (int i = 0; i < quantity; ++i) { //Sums all elements of the array sum += values[i]; } cout << sum/quantity << ".\n"; return EXIT_SUCCESS; }