Hi,

My program takes any polynomial and calculates the first and second derivatives and evaluates them at a given x. The coefficients of the polynomial are stored in a vector A. I now want to do something else to this polynomial, however my calculations for the derivatives mean that the values of A[0], A[1], A[2] etc change, so I was wondering is there any way of resetting them back to what they were without the user having to input them again?

Thanks in advance!

#include <iostream>
#include <vector>

using namespace std;

//Declare functions
void poly(vector<double> A, double x, double& p, double& px, double& pxx, int degree);

//---------------------------------------------------------------------------------------

int main()
{
    vector<double> A;                  // vector of coefficients a^degree...a^0
    int degree;                        // highest degree
    int i;
    double x;                          // value of x
    double p;                          // p(x)
    double px;                         // p'(x)
    double pxx;                        // p''(x)

    poly(A, x, p, px, pxx, degree);

    return 0;
}

//---------------------------------------------------------------------------------------

void poly(vector<double> A, double x, double& p, double& px, double& pxx, int degree)
{
    cout << "For polynomial p(x) = a0 + a1x + a2x^2 + ... + anx^n" << endl;
    cout << "Enter the polynomial degree, n" << endl;
    cout << "Example:  2 for a quadratic, 3 for a cubic..." << endl;
    cout << "Degree: ";
    cin >> degree;

    A.resize (degree + 1);

    for (int i = degree; i >= 0; i--)
    {
        cout << "Enter coefficient a" << i << ": ";
        cin >> A[i];
    }

    cout << "Enter the value of x for which to solve: ";
    cin >> x;

    //Calculations for p(x)

    p = A[degree];
    for (int i = (degree - 1); i >= 0; i--)
    {
        p = p*x;
        p = p+A[i];
    }

    //First differentiation on the coefficients
    for (int i = 1; i <= degree; i++)
    {
        A[i - 1] = i * A[i];
    }

    //Horner's Method - calculations for p'(x)
    px = A[degree - 1];
    for (int i = (degree - 2); i >= 0; i--)
    {
        px = px*x;
        px = px+A[i];
    }

    //Second differentiation on the coefficients
    for (int i = 1; i <= degree-1 ; i++)
    {
        A[i - 1] = i * A[i];
    }

    //Horner's Method - calculations for p''(x)
    pxx = A[degree - 2];
    for (int i = (degree - 3); i >= 0; i--)
    {
        pxx = pxx*x;
        pxx = pxx+A[i];
    }

    cout << "  p\(" << x << ") = " << p << endl;
    cout << " p\'(" << x << ") = " << px << endl;
    cout << "p\''(" << x << ") = " << pxx << endl;
}

If you want to be able to roll back changes, store the contents of the vector in a backup vector before making the changes. Alternatively, you could reverse your algorithm to obtain the original values, but the backup vector is easier and faster (at the cost of extra memory usage).

Thank you so much Narue that worked a treat :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.