如何用动态数组替换静态数组?

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

我实现了静态数组,如何将其转换为动态数组?

我已经完成了这个任务: 编写一个程序,用给定的方法在区间[a,b]上逼近函数f(x),m是函数已知的点数(table的大小)。函数类型设置为获取 table (xi, yi), i=1,2,..., m 的值并检查近似的质量。 我在一个单独的函数中设计了这个问题的解决方案,但是我不能使用动态数组重写这个任务。

请告诉我如何使用动态数组解决这个问题?

#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;
}

这是一个很好的起点。不确定我是否理解您的代码,但是可以编译:

旁注:

  • 如果你使用C++,那么就不是<math.h>而是<cmath>
  • 不推荐使用单字母标识符(变量和函数)(迭代整数除外),您应该使用有意义的名称。
#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;
}