0

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;
}
5
  • Do you have to create your own or would a standard well tested std::vector<double> do? Commented Jun 3, 2021 at 11:53
  • need a standard dynamic array Commented Jun 3, 2021 at 11:54
  • 4
    You tag as C++ but your programming style is clearly C (except for cout). If you use C++, then use the standard library containers, which provide ... "dynamic arrays", see en.cppreference.com/w/cpp/container/vector Commented Jun 3, 2021 at 11:55
  • 1
    en.cppreference.com/w/cpp/container/vector is the standard dynamic array. Commented Jun 3, 2021 at 12:04
  • In C++, "a standard dynamic array" is spelled std::vector. Don't get fouled by the names. Commented Jun 3, 2021 at 12:08

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I read the question as double D[15] being the static array OP refers to. I could be wrong.
@acraig5075 Mmmh, could be, yes. I don't really see what that magic value refers to, anyway. Must probably refer to the size of inputs. But this is just a starting point, so he can expand on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.