В одномерном массиве, состоящем из n вещественных элементов, вы-числить: 1) количество элементов массива, больших С; 2) произведение элементов массива, расположенных после макси-мального по модулю элемента. 3) Преобразовать массив таким образом, чтобы сначала располагались все отрицательные элементы, а потом – все положительные (элементы, равные 0 , считать положительными) — C++(Си)

#include <iostream>
 
int mod(int a)     { return a > 0 ? a : -a; }
bool    greate(int a, int b){ return a > b; }
int     mul(int a, int b)      { return a * b; }
 
unsigned count_if(const int *beg, const int *end, bool (*func)(int, int), int cmp)
{
    unsigned res = 0;
    for(const int *i = beg; i != end; ++i)
        if(func(*i, cmp)) ++res;
 
    return res;
}
 
int accumulate(const int *beg, const int *end, int val, int (*func)(int, int))
{
    int res = val;
    for(const int *i = beg; i != end; ++i)
        res = func(res, *i);
    return res;
}
 
int *maxmod(const int *beg, const int *end)
{
    int max = *(beg);
    int *res = const_cast<int*> (beg);
    for(const int *i = (beg + 1); i != end; ++i)
    {
        if(mod(max) < mod(*i))
        {
            max = *i;
            res = const_cast<int*> (i);
        }
    }
    return res;
}
 
 
void sort(int *beg, int *end){
    for(int *i = beg; i != end; ++i)
    {
        for(int *j = beg; j != (end - 1); ++j)
        {
            if(*j > *(j + 1)){
                int buff = *j;
                *(j) = *(j + 1);
                *(j + 1) = buff;
            }
        }
    }
}
 
 
int main(){
 
    const int n = 10;
    int arr[n] = {4, 2, 0, -4, 5, 6, -7, -12, 9, 9}; // max 12, 9 * 9 = 81
 
    int c = 7; // std::cin >> c;
 
    // 1
    std::cout << "Count elem > c : " << count_if(arr, arr + n, greate, c) << '\n';
 
    //2
    const int *maxpos = maxmod(arr, arr + n);
    std::cout << "mul: " << accumulate(maxpos + 1, arr + n, 1, mul) << '\n';
 
    //3
    sort(arr, arr + n);
 
    for(int i = 0; i < n; ++i)
        std::cout << arr[i] << " ";
 
    return 0;
}

Leave a Comment