Untitled

 avatar
ketronix
c_cpp
2 years ago
1.3 kB
2
Indexable
Never
#include <iostream>
#include <algorithm>
#include <cmath>
#include <malloc.h>

int main()
{

    double* arr = new double[]{1.2, 3.4, -5.6, 7.8, 9.0, -11.1, 12.2, -13.3};

    int n = malloc_size(arr) / sizeof(arr[0]);

    double max_elem = *std::max_element(arr, arr + n);

    std::cout << "Max element: " << max_elem << std::endl;

    double sum = 0;

    int last_positive_index = -1;

    for (int i = 0; i < n; i++)
    {

        if (arr[i] > 0)
        {

            last_positive_index = i;
        }
        if (i <= last_positive_index)
        {

            sum += arr[i];
        }
    }
    std::cout << "Sum of elements before last positive element: " << sum << std::endl;

    double a, b;

    std::cout << "Enter interval [a, b]: ";

    std::cin >> a >> b;

    double compressed_arr[n];

    int compressed_size = 0;

    for (int i = 0; i < n; i++)
    {

        if (fabs(arr[i]) < a || fabs(arr[i]) > b)
        {

            compressed_arr[compressed_size] = arr[i];

            compressed_size++;
        }
    }
    std::cout << "Compressed array: ";

    for (int i = 0; i < compressed_size; i++)
    {

        std::cout << compressed_arr[i] << " ";
    }
    std::cout << std::endl;

    for (int i = compressed_size; i < n; i++)
    {

        compressed_arr[i] = 0;
    }


    return 0;

}