Advertisement
vencinachev

Zadacha2

Dec 16th, 2020
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #define EPS 0.001
  4.  
  5. using namespace std;
  6.  
  7. bool func(double a[], int& n, double b[], int& nb);
  8.  
  9. int main()
  10. {
  11.     int n, nb;
  12.     cout << "Enter count: ";
  13.     cin >> n;
  14.     double a[n], b[n];
  15.     cout << "Enter numbers: ";
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         cin >> a[i];
  19.     }
  20.  
  21.     bool isGeometricProg = func(a, n, b, nb);
  22.  
  23.     cout << "Arithmetic progression: ";
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         cout << a[i] << " ";
  27.     }
  28.     cout << endl;
  29.     cout << "Filtered elements (FE): ";
  30.     for (int i = 0; i < nb; i++)
  31.     {
  32.         cout << b[i] << " ";
  33.     }
  34.     cout << endl;
  35.  
  36.     cout << boolalpha << "Is geometric progression (FE): " << isGeometricProg << endl;
  37.     return 0;
  38. }
  39.  
  40. bool func(double a[], int& n, double b[], int& nb)
  41. {
  42.     double prevG, q;
  43.     int cntExtract = 0;
  44.     bool isGeom = true;
  45.  
  46.     double d = a[1] - a[0];
  47.     int j = 2;
  48.  
  49.     for (int i = 2; i < n; i++)
  50.     {
  51.         if (fabs(a[j - 1] + d - a[i]) < EPS)  // a[j - 1] + d == a[i]
  52.         {
  53.             a[j] = a[i];
  54.             j++;
  55.         }
  56.         else
  57.         {
  58.             b[cntExtract] = a[i];
  59.             cntExtract++;
  60.             if (cntExtract == 1)
  61.             {
  62.                 prevG = a[i];
  63.             }
  64.             else if (cntExtract == 2)
  65.             {
  66.                 q = a[i] / prevG;
  67.                 prevG = a[i];
  68.             }
  69.             else
  70.             {
  71.                 if (!(fabs(prevG * q - a[i]) < EPS)) // prevG * q != a[i]
  72.                 {
  73.                     isGeom = false;
  74.                 }
  75.                 prevG = a[i];
  76.             }
  77.         }
  78.     }
  79.     n = j;
  80.     nb = cntExtract;
  81.     if (cntExtract < 2)
  82.     {
  83.         return false;
  84.     }
  85.     return isGeom;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement