#include // std::cout #include // random number generator /* * Attention: Never do it like this! * * This code contains a serious bug in memory handling, a memory leak. * There are different possible ways to fix that, which we can discuss. */ namespace { int iterations = 1000; int output_frequency = 100; constexpr int array_size = 1'000'000; std::subtract_with_carry_engine generator(0); } // use the random number from the standard library unsigned* create_random_array() { unsigned* data = new unsigned[array_size]; for(int i = 0; i < array_size; i++) data[i] = generator(); return data; } // compute average of an array with random numbers double crunch_numbers(unsigned* data) { long long sum = 0; for(int i = 0; i < array_size; i++) sum += data[i]; return (double)sum / array_size; } int main() { for(int k = 0; k < iterations; k++) { double avg = crunch_numbers(create_random_array()); if((k+1) % output_frequency == 0) std::cout << k+1 << "\t" << avg << "\n"; } }