Untitled

 avatar
unknown
plain_text
4 years ago
2.8 kB
6
Indexable
#include <iostream>
#include <string.h>
using std::cout;
using std::cin;
using std::endl;

// Функция для заполнения матрицы.
void input_array(int n, double *array) {
    cout << "=====================================" << endl;
    //Ввод элементов массива
    for (int i = 0; i < n; i++) {
        cout << "(Ввод массива) M[" << i << "] : ";
        cin >> array[i];
    }
    cout << "=====================================" << endl;
}

//
int A_to_B_elem (int n, double *array) {
    double A,B; int count = 0;
    cout << "1) (Диапазон).Введите A: ";
    cin >> A;
    cout << "1) (Диапазон).Введите B: ";
    cin >> B;
    for (int i = 0; i < n; i++) {
        if (array[i] > A and array[i] < B) {
            count+=1;
        }
    }
    cout << "=====================================" << endl;
    return count;
}
void bubble_sort(int n, double *array_sorted) {
    double mem = 0;
    int count;
    for (int k = 1; k <= n-1; k++) {
        count = 0;
        for (int i = 0; i < n-1; i++) {
            if (array_sorted[i+1] < array_sorted[i]) {
                mem = array_sorted[i];
                array_sorted[i] = array_sorted[i+1];
                array_sorted[i+1] = mem;
            }
            else {
                count+=1;
            }
        }
        if (count == n-1) {
            break;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << "(Вывод сорт.массива) M[" << i << "] : " << array_sorted[i] << endl;
    }
}
// Сумма элементов после макс.элемента массива
double sum_after_max(int n, int max, double *array) {
    double sum = 0;
    bool start_sum = false;
    for (int i = 0; i < n; i++) {
        if (start_sum) {
            sum+=array[i];
        }
        if (array[i] == max) {
            start_sum = true;
        }
    }
    return sum;
}
int main() {
    int count;
    int n;
    cout << "=====================================" << endl ;
    cout << "Кол-во элементов n : ";
    cin >> n;
    double array[n]; //Обьявление массива с заданным количеством n
    double array_sorted[n];
    input_array(n, array);
    memcpy(array_sorted, array, sizeof(array));
    count = A_to_B_elem (n, array);
    cout << "Количество элементов в диапазоне: " << count << endl;
    cout << "=====================================" << endl ;
    bubble_sort(n, array_sorted);
    double max_elem = array_sorted[n-1];
    double sum = sum_after_max(n,max_elem, array);
    cout << "=====================================" << endl ;
    cout << "Сумма элементов после макс.элемента равна " << sum << endl;
    return 0;
}
Editor is loading...