#include #include #include #include #include #include #include int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << "Please, give the number of values to compute in the command " "line.\n"; return 1; } int N = std::stoi(argv[1]); if (N <= 0) { std::cerr << "The number of values must be positive.\n"; return 2; } // Random number generation. std::random_device rd; std::default_random_engine rand_gen(rd()); std::uniform_int_distribution<> dist(0, std::numeric_limits::max()); // Create a random array of N int elements. std::vector array(N); std::generate(begin(array), end(array), [&]() { return dist(rand_gen); }); int max, min; std::thread calc_min([&min, &array = std::as_const(array)]() { min = array[0]; for (size_t i = 1; i < array.size(); i++) if (array[i] < min) { min = array[i]; } }); std::thread calc_max([&max, &array = std::as_const(array)]() { max = array[0]; for (size_t i = 1; i < array.size(); i++) if (array[i] > max) { max = array[i]; } }); calc_min.join(); calc_max.join(); std::cout << "Generated " << N << " random numbers." << std::endl; std::cout << "Largest is " << max << std::endl << "Smallest is " << min << std::endl; return 0; }