I've put so much effort into making this program and it keeps crashing! I previously had the two functions in separate files which were working fine, but I combined them into 2 - the poly function is still working fine, but the bisection function crashes at the line:

"Enter the maximum number of iterations allowed: "

I really don't have a clue why this is happening, can anyone PLEASE explain?

Thanks in advance!

#include <iostream>
#include <vector>

using namespace std;

//Declare functions
void poly(vector<double> A, vector<double> B, double x, double& p, double& px, double& pxx, int degree);
void bisection(vector<double> B, double left_endpoint, double right_endpoint, double epsilon, double p, int max, int degree);
double f(vector<double>, double, double, int);

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

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

    int max = 0;                       // maximum number of iterations
    double left_endpoint = 0.0;        // left endpoint of original interval
    double right_endpoint = 0.0;       // right endpoint of the original interval
    double epsilon = 0.000001;         // convergence criterion

    poly(A, B, x, p, px, pxx, degree);
    bisection(B, left_endpoint, right_endpoint, epsilon, p, max, degree);

    return 0;
}

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

void poly(vector<double> A, vector<double> B, 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);
    B.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--)
    {
        B[i] = 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;
}

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

void bisection(vector<double> B, double left_endpoint, double right_endpoint, double epsilon, double p, int max, int degree)
{
    cout << "\nEnter the left endpoint of the original search interval (a): ";
    cin >> left_endpoint;
    cout << "Enter the right endpoint of the original search interval (b): ";
    cin >> right_endpoint;
    cout << "Enter the maximum number of iterations allowed: ";
    cin >> max;

    double x1 = 0.0; // left endpoint of current interval
    double x2 = 0.0; // right endpoint of current interval
    double xmid = 0.0; // computed midpoint of current interval
    double f1 = 0.0; // function evaluated at left endpoint of current interval
    double f2 = 0.0; // function evaluated at right endpoint of current interval
    double fmid = 0.0;; // function evaluated at computed midpoint of current interval
    double width = 0.0; // width of original interval (b - a)
    double current_width = 0.0; // width of current interval (xmid - x1)

    x1 = left_endpoint;

    f1 = f(B, x1, p, degree);

    xmid = right_endpoint;

    fmid = f(B, xmid, p, degree);

    width = (right_endpoint - left_endpoint);

// verify there is a root in the interval
    if (f1 * fmid > 0.0)
    {
        cout << "\nNo root in the original interval exists" << endl;
    }
    else
    {
        for (int i = 1; i <= max; i++)
        {
            x2 = (x1 + xmid) / 2.0;
            cout << "The next midpoint, approximation is " << x2 << endl;
            f2 = f(B, x2, p, degree);
    if (f1 * f2 <= 0.0) // root is in left half interval
    {
        current_width = (x2 - x1) / 2.0;
        fmid = f2;
        xmid = x2;
    }
    else // root is in right half interval
    {
        current_width = (xmid - x2) / 2.0;
        f1 = f2;
        x1 = x2;
    }
    if (current_width < epsilon)
    {
        cout << "\nA root at x = " << x2 << " was found "
        << "in " << i << " iterations" << endl;
        f2 = f(B, x2, p, degree);
        cout << "The value of the function at the root is " << f2 << endl;
        return;
}
}
}

cout << "***** BISECTION METHOD *****"
<< "\nAfter " << max << " iterations, no root was found "
<< "within the convergence criterion\n"
<< "The search for a root has failed due to excessive iterations\n"
<< "after the maximum number of " << max << " iterations\n"
<< "Please try again." << endl;

return;
}// end of bisection

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

double f(vector<double> B, double x, double p, int degree)// function to evaluate p(x)
{
    p = B[degree];
    for (int i = (degree - 1); i >= 0; i--)
    {
        p = p*x;
        p = p+B[i];
    }

return p;
}// end of f

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

Recommended Answers

All 6 Replies

How many iterations are you putting in? It may be greater than the maximum value for an integer on your machine ( I only say that because you were starting at the millions before).

Haha no I wasn't doing millions this time, I learn't my lesson! I tested it for 100 this time... maybe my laptop is just crap?!

I knew when I wrote that that it probably was not right. It has nothing to do with your iterations count but it's crashing in your f() method. You're passing in degree and it's garbage (I think because you needed to pass it in by reference to the poly function) and your actual B vector is of 0 length. EDIT: nevermind lol

Summary of all of the above:
change poly to

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

(and if you need A for another segment of your program change it to a reference too)

You are VERY kind for taking the time to look at that for me, I can't thank you enough.

You're very welcome. Good luck with it.

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.