Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- int main()
- {
- const int arrSize{10};
- int arr[arrSize]{ };
- int idxMin{0};
- int idxMax{0};
- int negSum{0};
- int negFLSum{0};
- int evenProd{1};
- int minMaxProd{0};
- int firstNegIdx{-1};
- int lastNegIdx{-1};
- srand(time(0));
- std::cout << "Index\tValue\n";
- for (int idx{0}; idx < arrSize; ++idx)
- {
- std::cout << idx << "\t" << (arr[idx] = -5 + rand() % 11)<< '\n';
- }
- for (int idx{0}; idx < arrSize; ++idx)
- {
- if (arr[idx] < 0) { negSum += arr[idx]; lastNegIdx = idx; };
- if (arr[idx] < 0 and firstNegIdx < 0) { firstNegIdx = idx;}
- if (arr[idx] < arr[idxMin]) { idxMin = idx; }
- if (arr[idx] > arr[idxMax]) { idxMax = idx; }
- if (idx % 2 == 0) { evenProd *= arr[idx]; }
- }
- if (idxMin > idxMax ) { int tmp{idxMax}; idxMax=idxMin; idxMin=tmp;}
- if ( idxMax - idxMin != 1)
- {
- minMaxProd = 1;
- for (int idx{idxMin + 1}; idx < idxMax; ++idx)
- {
- minMaxProd *= arr[idx];
- }
- }
- for (int idx{firstNegIdx + 1}; idx < lastNegIdx; ++idx)
- {
- negFLSum += arr[idx];
- }
- std::cout << "Sum of negative elements: " << negSum << '\n';
- std::cout << "Product of elements between min and max elem. : " << minMaxProd << '\n';
- std::cout << "Product of even number elements: " << evenProd << '\n';
- std::cout << "Sum between first = " << arr[firstNegIdx] <<
- " and last = "<< arr[lastNegIdx] << " negative elements: " << negFLSum << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment