每次我尝试运行代码的这一部分时,c ++都会崩溃运行

时间:2017-03-29 02:55:43

标签: c++

一旦我添加了功能区分并在main方法中调用它,它就开始崩溃了。在添加此方法之前,代码运行正常。代码应该提供执行多项式,然后使用多项式的数组找到它的导数。

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int readPolynomial (int maxOrder, double polynomial[]) {
     int order,i;
    cout<< "enter the order of the polynomial";
    cin>>order;
    i=order;
    while (i>-1){
        cout<<" enter a coeffecient for x^"<<i<<endl;
        cin>>polynomial[i];
        --i;
    }
    return order;
}

void displayPolynomial (int order, double polynomial[]) {
    for (int i = order; i>-1;i--){
        if (polynomial[i] > 0) {
                 cout << " +" << polynomial[i]<< "x^"<<i;       
        } 
        else {
            cout << " - " << fabs(polynomial[i])<<"x^"<<i;
        } 
    }
}
int differentiate (int order, double polynomial[], double derivative[]){
    int der;
    der= order-1;
    int i = der;
    while (i>-1){
        derivative[i]=polynomial[order]*order;
        order--;
        i--;
    }
    return der; 
}
int main () {

    const int MAXN = 8; // maximum order (a constant)
    double polynomial[MAXN + 1];
    int polynomialN;
    double x,value;
    double derivative[polynomialN-1];
    double derOrder;

    polynomialN = readPolynomial (MAXN, polynomial);
    cout << "\nThe polynomial (order = " << polynomialN << ") is\n";
    displayPolynomial (polynomialN, polynomial);
    derOrder = differentiate (polynomialN, polynomial, derivative);

    cout<< " the derivative of the polynomial is:"<<endl;
    displayPolynomial (derOrder, derivative);

    system("PAUSE"); return 0;
}

0 个答案:

没有答案