Другие языки программирования и технологии

Помогите пожалуйста сдать зачет по программированию на с++!

Дан массив из 3 элементов. Заполнить его случайными числами от -100 до 100. Вывести его на экран. Найти максимальное число массива среди отрицательных элементов и минимальное число среди положительных и вывести их на экран. Обращение к элементам массива реализовать через указатель.
#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>
#include <random>
using namespace std;
constexpr auto a = -100;
constexpr auto b = 100;
int integer() {
const uniform_int_distribution<> uid(a, b);
random_device rd;
mt19937 gen(rd());
return uid(gen);
}
int main() {
array<int, 3U> box;
generate(box.begin(), box.end(), integer);
copy(box.begin(), box.end(), ostream_iterator<int>(cout, " "));
auto[min, max] = minmax_element(box.begin(), box.end());
cout << "\nmin: " << *min << "\nmax: " << *max << endl;
system("pause");
}
Рафаэль Гилязов
Рафаэль Гилязов
88 430
Лучший ответ
#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>

using namespace std;

int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
srand(time(NULL));
system("color 0A");

size_t len = 3u;
int left = -100,
right = 100;
auto a = new int[len];
cout << "Исходный массив" << endl;
for (auto it = a; it != a + len; ++it)
{
int value = left + rand() % (right - left + 1u);
cout << value << " ";
*it = value;
}
cout << endl;

int min_ = 101,
max_ = -101;
auto flag_p = false,
flag_n = false;
for (auto it = a; it != a + len; ++it)
{
max_ = *it < 0 && *it > max_ ? *it : max_;
min_ = *it > 0 && *it < min_ ? *it : min_;
flag_p = !flag_p && *it > 0? true : flag_p;
flag_n = !flag_n && *it < 0? true : flag_n;
}
cout <<
(flag_p ? "min_ = " + to_string(min_) : "Положительных чисел нет")
<< endl;
cout <<
(flag_n ? "max_ = " + to_string(max_) : "Отрицательных чисел нет")
<< endl;

system("pause");
return 0;
}
AP
Alexander Pyagay
8 552
const std::array arr;
std::generate(arr.begin(), arr.end(), [](){ return std::rand() % 201 - 100; });
int max = 0;
int min = 0;
for (const auto i : arr) {
if (i < 0 && ((max == 0) || (i > max))) max = i;
if (i > 0 && ((min == 0) || (i < min))) min = i;
}
std::cout << max << '\n';
std::cout << min << '\n';
Еркен Сисенов
Еркен Сисенов
2 223