Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #define EPS 0.001
- using namespace std;
- bool func(double a[], int& n, double b[], int& nb);
- int main()
- {
- int n, nb;
- cout << "Enter count: ";
- cin >> n;
- double a[n], b[n];
- cout << "Enter numbers: ";
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- bool isGeometricProg = func(a, n, b, nb);
- cout << "Arithmetic progression: ";
- for (int i = 0; i < n; i++)
- {
- cout << a[i] << " ";
- }
- cout << endl;
- cout << "Filtered elements (FE): ";
- for (int i = 0; i < nb; i++)
- {
- cout << b[i] << " ";
- }
- cout << endl;
- cout << boolalpha << "Is geometric progression (FE): " << isGeometricProg << endl;
- return 0;
- }
- bool func(double a[], int& n, double b[], int& nb)
- {
- double prevG, q;
- int cntExtract = 0;
- bool isGeom = true;
- double d = a[1] - a[0];
- int j = 2;
- for (int i = 2; i < n; i++)
- {
- if (fabs(a[j - 1] + d - a[i]) < EPS) // a[j - 1] + d == a[i]
- {
- a[j] = a[i];
- j++;
- }
- else
- {
- b[cntExtract] = a[i];
- cntExtract++;
- if (cntExtract == 1)
- {
- prevG = a[i];
- }
- else if (cntExtract == 2)
- {
- q = a[i] / prevG;
- prevG = a[i];
- }
- else
- {
- if (!(fabs(prevG * q - a[i]) < EPS)) // prevG * q != a[i]
- {
- isGeom = false;
- }
- prevG = a[i];
- }
- }
- }
- n = j;
- nb = cntExtract;
- if (cntExtract < 2)
- {
- return false;
- }
- return isGeom;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement