Untitled

 avatar
ketronix
c_cpp
3 years ago
1.3 kB
6
Indexable
#include <iostream>
#include <malloc.h>

using namespace std;

int sumBetweenFirstAndLast(float arr[], int size) {
    int sum = 0;
    bool firstPositiveFound = false;
    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            if (!firstPositiveFound) {
                firstPositiveFound = true;
            } else {
                sum += arr[i];
            }
        }
    }
    return sum + 1;
}

void reorder(float arr[], int n)
{
    int i = -1, temp = 0;
    for (int j = 0; j < n; j++) {
        if (arr[j] == 0) {
            i++;
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}

int main() {

    float* array = new float[]{ -1, 1, 2, 4, 5, 7};
    int count = malloc_usable_size(array) / sizeof(float);
    cout << "Elements in array: " << count << endl;

    float min = array[0];
    for (int i = 0; i <= count; ++i) {
        if(min > array[i])
        {
            min = array[i];
        }
    }

    auto sum = sumBetweenFirstAndLast(array, count);
    reorder(array, count);

    cout << "Sum of positive numbers: " << sum << endl;
    cout << "Minimal element in array: " << min << endl;

    cout << "New Array: ";
    for (int i = 0; i < count; i++)
        cout << array[i] << " ";
    return 0;
}
Editor is loading...