Top.Mail.Ru
Ответы

Дан массив из 10 вещественных чисел. Найдите все максимальные по модулю элементы массива.

Дата
Популярность
Аватар пользователя
1 год назад
12345678910111213141516171819202122232425
 #include <algorithm> 
#include <array> 
#include <cmath> 
#include <iomanip> 
#include <iostream> 
#include <random> 
using namespace std; 
using array10_t = array<int, 10>; 
int main() { 
    array10_t seq{}; 
    uniform_int_distribution<> uid(-3, 3); 
    mt19937 gen{ random_device()() }; 
    for (auto& x : seq) x = uid(gen); 
    constexpr streamsize w = 5; 
    for (auto x : seq) cout << setw(w) << x; 
    cout.put('\n'); 
    auto pred = [](int a, int b) { return fabs(a) < fabs(b); }; 
    auto mmax = *max_element(seq.begin(), seq.end(), pred); 
    for (auto x : seq) { 
        cout << setw(w); 
        if (fabs(x) == fabs(mmax)) cout << '^'; 
        else cout << ' '; 
    } 
    puts(""); 
}