Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- void print(double *x, int n);
- double negativeIndex(double *x, int n);
- double sumNegative(double *x, int n, int ind);
- void sort(double *x, int n);
- int main() {
- setlocale(LC_ALL, "rus");
- srand(time(NULL));
- int n, nInd;
- cout << "Введите кол-во элементов массива: ";
- cin >> n;
- double *arr = new double[n];
- for (int i = 0; i < n; i++)
- arr[i] = (((double)rand() / RAND_MAX) * 4) - 2;
- cout << "Вывод массива: " << endl;
- print(arr, n);
- nInd = negativeIndex(arr, n);
- if (nInd != -1)
- cout << "Сумма элементов после отр. эл. до другого отр. эл. или до конца: " << sumNegative(arr, n, nInd) << endl << endl;
- else
- cout << "Отрицательных чисел нету!" << endl << endl;
- sort(arr, n);
- cout << "Отсортированный массив: " << endl;
- print(arr, n);
- delete[] arr;
- return 0;
- }
- void print(double *x, int n) {
- for (int i = 0; i < n; i++)
- cout << x[i] << ' ';
- cout << endl;
- }
- double negativeIndex(double *x, int n) {
- int ind = -1;
- for (int i = 0; i < n; i++) {
- if (x[i] < 0) {
- ind = i;
- break;
- }
- }
- return ind;
- }
- double sumNegative(double *x, int n, int ind) {
- double s = 0;
- for (int i = ind + 1; i < n; i++) {
- if (!(x[i] < 0))
- s += x[i];
- else break;
- }
- return s;
- }
- void sort(double *x, int n) {
- for (int i = 1; i < n; i++) {
- int cur = i;
- while (!(abs(x[cur - 1]) < 1) && abs(x[cur]) < 1 && cur > 0) {
- swap(x[cur - 1], x[cur]);
- cur--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment