avr39ripe

arrSumFLNegEvenIdxProd

Oct 29th, 2020 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. int main()
  4. {
  5.     const int arrSize{10};
  6.     int arr[arrSize]{ };
  7.  
  8.     int idxMin{0};
  9.     int idxMax{0};
  10.     int negSum{0};
  11.     int negFLSum{0};
  12.     int evenProd{1};
  13.     int minMaxProd{0};
  14.     int firstNegIdx{-1};
  15.     int lastNegIdx{-1};
  16.  
  17.     srand(time(0));
  18.  
  19.     std::cout << "Index\tValue\n";
  20.     for (int idx{0}; idx < arrSize; ++idx)
  21.     {
  22.         std::cout << idx << "\t" << (arr[idx] = -5 + rand() % 11)<< '\n';
  23.     }
  24.  
  25.     for (int idx{0}; idx < arrSize; ++idx)
  26.     {
  27.         if (arr[idx] < 0) { negSum += arr[idx]; lastNegIdx = idx; };
  28.         if (arr[idx] < 0 and firstNegIdx < 0) { firstNegIdx = idx;}
  29.  
  30.         if (arr[idx] < arr[idxMin]) { idxMin = idx; }
  31.         if (arr[idx] > arr[idxMax]) { idxMax = idx; }
  32.  
  33.         if (idx % 2 == 0) { evenProd *= arr[idx]; }
  34.     }
  35.  
  36.     if (idxMin > idxMax ) { int tmp{idxMax}; idxMax=idxMin; idxMin=tmp;}
  37.     if ( idxMax - idxMin != 1)
  38.     {
  39.         minMaxProd = 1;
  40.         for (int idx{idxMin + 1}; idx < idxMax; ++idx)
  41.         {
  42.             minMaxProd *= arr[idx];
  43.         }
  44.     }
  45.  
  46.     for (int idx{firstNegIdx + 1}; idx < lastNegIdx; ++idx)
  47.     {
  48.         negFLSum += arr[idx];
  49.     }
  50.  
  51.     std::cout << "Sum of negative elements: " << negSum << '\n';
  52.     std::cout << "Product of elements between min and max elem. : " << minMaxProd << '\n';
  53.     std::cout << "Product of even number elements: " << evenProd << '\n';
  54.     std::cout << "Sum between first = " << arr[firstNegIdx] <<
  55.             " and last = "<< arr[lastNegIdx] << " negative elements: " << negFLSum << '\n';
  56.  
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment