dan
dan

Reputation: 3

how do I replace a static array with a dynamic one?

I implemented a static array, how can I convert it to a dynamic array?

I have completed this task: Write a program for approximating the function f(x) on the interval [a, b] by a given method, m is the number of points at which the function is known (the size of the table). The function type is set to get the values of the table (xi, yi), i=1,2,..., m and check the quality of the approximation. I designed the solution to this problem in a separate function, but I can't rewrite this task using a dynamic array.

Please tell me how to solve this problem using a dynamic array?

#include <iostream>
#include <math.h>
using namespace std;

double f(double x) {
    return x * x * x - 5 * x * x;
}
double N(double* x, double* y, double xt, int m) {
    int i, k;
    double D[15], N, p;
    for(i = 0; i < m; i++) {
        D[i] = y[i];
    }
    p = 1;
    N = y[0];
    for(k = 0; k < m - 1; k++) {
        for(i = 0; i < m - k; i++) {
            D[i] = (D[i] - D[i + 1]) / (x[i] - x[i + k + 1]);
        }
        p = p * (xt - x[k]);
        N += p * D[0];
    }
    return N;
}
double pogr(double x, double N) {
    return (f(x) - N);
}

int main() {
    double a, b, h, h1, x, *m_x, m_y_t, *m_y, max, p = 0;
    int i, n, m;
    cout << " condition: a=-2, b=5, m=5." << endl;
    cout << "enter the beginning of the segment: ";
    cin >> a;
    cout << "enter the end of the segment: ";
    cin >> b;
    cout << "enter the number of known nodes: ";
    cin >> m;
    cout << "enter the number of points to restore the value to: ";
    cin >> n;
    h = (b - a) / (m - 1);
    h1 = (b - a) / (n - 1);
    m_y = new double[m + 1];
    m_x = new double[m + 1];

    for(x = a, i = 0; i < m; i++) {
        m_x[i] = x;
        m_y[i] = f(x);
        x += h;
    }
    max = p;
    for(x = a, i = 0; i < n; i++, x += h1) {
        m_y_t = N(m_x, m_y, x, m);
        cout << "xt= " << x << " f*(x)= " << m_y_t;
        p = abs(f(x) - m_y_t);
        cout << "\terror rate: " << p << endl;
        if(p > max) max = p;
    }
    cout << " мах error rate = " << max << endl;
}

Upvotes: 0

Views: 187

Answers (1)

kebs
kebs

Reputation: 6707

Here you have a good starting point. Not sure I understand your code, but this compiles:

Side notes:

  • if you use C++, then it is not <math.h> but <cmath>
  • single letter identifiers (variables and functions) is not recommended (except maybe for iterating ints), you should use significant names.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

double f(double x) {
    return x * x * x - 5 * x * x;
}

double N( const std::vector<double>& x, const std::vector<double>& y, double xt, int m) {
    int i, k;
    double D[15], N, p;
    for(i = 0; i < m; i++) {
        D[i] = y[i];
    }
    p = 1;
    N = y[0];
    for(k = 0; k < m - 1; k++) {
        for(i = 0; i < m - k; i++) {
            D[i] = (D[i] - D[i + 1]) / (x[i] - x[i + k + 1]);
        }
        p = p * (xt - x[k]);
        N += p * D[0];
    }
    return N;
}
double pogr(double x, double N) {
    return (f(x) - N);
}

int main() {
    double a, b, m_y_t, max, p = 0;
    int n, m;
    cout << " condition: a=-2, b=5, m=5." << endl;
    cout << "enter the beginning of the segment: ";
    cin >> a;
    cout << "enter the end of the segment: ";
    cin >> b;
    cout << "enter the number of known nodes: ";
    cin >> m;
    cout << "enter the number of points to restore the value to: ";
    cin >> n;

    std::vector<double> m_x(m+1);
    std::vector<double> m_y(m+1);
    
    double h = (b - a) / (m - 1);
    double h1 = (b - a) / (n - 1);

    for(int x = a, i = 0; i < m; i++) {
        m_x[i] = x;
        m_y[i] = f(x);
        x += h;
    }
    max = p;
    for(int x = a, i = 0; i < n; i++, x += h1)
    {
        m_y_t = N(m_x, m_y, x, m);
        cout << "xt= " << x << " f*(x)= " << m_y_t;
        p = abs(f(x) - m_y_t);
        cout << "\terror rate: " << p << endl;
        if(p > max) max = p;
    }
    cout << " мах error rate = " << max << endl;
}

Upvotes: 1

Related Questions