SHARE
TWEET

saspest; apr kur nepara; arp between 1 and last neg

parad0xxxxx Jan 24th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9.     int arraySize, randRangeFrom, randRangeTo;
  10.     cout << "Enter element count: ";
  11.     cin >> arraySize;
  12.     if (arraySize < 1) {
  13.         cout << "\nCount must be posivive integer.\n\n";
  14.         return 0;
  15.     }
  16.  
  17.     float* arr = new float[arraySize];
  18.     char v(0);
  19.     cout << "\n\nHow would You like to fill in array, (M)anually or (A)utomatically? \n:";
  20.     cin >> v;
  21.  
  22.     if (v == 'M') {
  23.         for (int i = 0; i < arraySize; i++)
  24.         {
  25.             cout << "Enter element " << i + 1 << ": ";
  26.             cin >> arr[i];
  27.         }
  28.  
  29.     }
  30.  
  31.     else if (v == 'A') {
  32.         cout << "Enter initial range value: ";
  33.         cin >> randRangeFrom;
  34.  
  35.         cout << "Enter end range value: ";
  36.         cin >> randRangeTo;
  37.         cout << endl;
  38.  
  39.         srand(time(NULL));
  40.         for (int i = 0; i < arraySize; i++)
  41.         {
  42.             arr[i] = (rand() % (randRangeTo * 10 - randRangeFrom * 10 + 1) + randRangeFrom * 10) / 10.0;
  43.         }
  44.  
  45.     }
  46.  
  47.     else {
  48.         return 0;
  49.  
  50.     }
  51.  
  52.  
  53.     for (int i = 0; i < arraySize; i++)
  54.     {
  55.         cout << arr[i] << "  ";
  56.     }
  57.  
  58.     cout << "\n\nTask 1.\n";
  59.  
  60.     // Aprēķināt masīva elementu summu, kuriem ir nepāra numurs. done.
  61.  
  62.     float sum = 0;
  63.     for (int i = 0; i < arraySize; i++)
  64.     {
  65.         if (i % 2 != 0)
  66.         {
  67.             sum += arr[i];
  68.         }
  69.     }
  70.     cout << "Sum of even elements is: " << sum;
  71.  
  72.     //Aprēķināt masīva elementu summu, kuri atrodas starp masīva pirmo un pēdējo negatīvo elementu.
  73.     cout << "\n\n\nTask 2.\n";
  74.  
  75.     float sumBetween = 0;
  76.     int firstNeg = 0, lastNeg = 0;
  77.     bool isFirstNegativeFound = false;
  78.     for (int i = 0; i < arraySize; i++) {
  79.         if (arr[i] < 0) {
  80.             if (isFirstNegativeFound) {
  81.                 lastNeg = i;
  82.             }
  83.             else {
  84.                 firstNeg = i;
  85.                 isFirstNegativeFound = true;
  86.             }
  87.         }
  88.     }
  89.  
  90.     for (int i = firstNeg + 1; i < lastNeg; i++) {
  91.         sumBetween += arr[i];
  92.     }
  93.  
  94.  
  95.     cout << "The sum of the elements between the first (" << firstNeg << ") and the last (" << lastNeg << ") negatives is: " << sumBetween;
  96.  
  97.     //"Saspiest" masīvu. Izdzēst no tā visus elementus, kuru modulis nav lielāks par 1. Atbrīvotus masīva beigās elementus aizpildīt ar 0.
  98.     cout << "\n\n\nTask 3.\n";
  99.  
  100.  
  101.     for (int i = 0; i < arraySize; i++)
  102.     {
  103.         if (fabs(arr[i]) < 1)
  104.         {
  105.             arr[i] = 0;
  106.         }
  107.  
  108.         if (arr[i] == 0)
  109.         {
  110.             for (int j = i + 1; j < arraySize; j++)
  111.                 if (arr[j] != 0 && arr[i] == 0)
  112.                 {
  113.                     float tmp = arr[j];
  114.                     arr[j] = arr[i];
  115.                     arr[i] = tmp;
  116.                 }
  117.  
  118.         }
  119.         cout << arr[i] << "  ";
  120.     }
  121.     cout << " \nArray compressed." << endl;
  122.  
  123.     delete[] arr;
  124.     return 0;
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top