I'm testing this program for:

x^2 + 0.17714x -2.5147583

with k = 1.4997

I've been looking at this for hours and have worked through it on paper, but can't figure out why line 59 is calculating C[1] as 0.17714. C[1] should be 1.6768.

Can anyone please help me figure this out?

Thanks in advance!

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

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

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

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

    poly(A, B, C, x, p, px, pxx, degree, k);

    return 0;
}

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

void poly(vector<double>& A, vector<double>& B, vector<double>& C, double x, double& p, double& px, double& pxx, int& degree, int k)
{
    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);
    B.resize (degree + 1);
    C.resize (degree + 1);

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

     for (int i = degree; i >= 0; i--)
    {
        C[i] = A[i];
    }

    cout<<endl<<"Enter Constant term(k) of Divisor (x-k)";
     cin>>k;

     for (int i = degree-1; i>0; i--)
     {
          C[i]=A[i]+k*C[i+1];

          cout<< C[i] << endl;
     }

}

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

>>x^2 + 0.17714x -2.5147583
with k = 1.4997

What does the k represent ?


Also for vectors you can just assign another vector by using the '=' assignment operator. If you want the reverse it, then use the std::reverse to reverse a vector then use the assignment operator, better practice.

Thanks for the advice firstPerson.

The program is for polynomial division, so I'm trying to divide the polynomial by factor (x - 1.4997) and hence k is just a constant for whatever the factor is.

>>x^2 + 0.17714x -2.5147583
with k = 1.4997

What does the k represent ?


Also for vectors you can just assign another vector by using the '=' assignment operator. If you want the reverse it, then use the std::reverse to reverse a vector then use the assignment operator, better practice.

This article has been dead for over six months. Start a new discussion instead.