### HELP [dynamic polynomial array]

Hello, I've been working on this code for a while now but can't get two functions to work and I'm unsure how to code them

The first function is an output operator

this is the code i have come up with for it in my cpp file
 ``123456789`` ``````ostream & operator<<(ostream & cout, const polynomial & r) { size_t i; for (i=0; i < r.degree() +1; i++) cout << r.coef[i] << "x^" << i; cout << endl; return cout; } ``````

The second function is my eval function, I'm trying to use pow for the first time to raise to exponents and doing a pretty awful job at it.

This is the code in my cpp file
 ``123456789`` `````` double polynomial::eval(double x) const { double p = 0; for ( int i = current_degree; i >= 0; i-- ) p = coef[i](pow(x,i)); return p; } ``````

Below is my header file with all function pre/post conditions

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121`` ``````// FILE: poly.h // CLASS PROVIDED: // class polynomial // A polynomial has one variable x, real number coefficients, and // non-negative integer exponents. Such a polynomial can be viewed // as having the form: // A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0] // where the A[n] are the real number coefficients and x^i represents // the variable x raised to the i power. The coefficient A[0] is // called the "constant" or "zeroth" term of the polynomial. // This version works by storing the coefficients in // a dynamic array. The coefficient for the x^k term is stored // in location [k] of the dynamic array. When a new term is // added beyond the current size of the array, then the // dynamic array is replaced by a new, larger array. // MEMBER CONSTANTS // const size_t DEFAULT_CAPACITY // The size of the initial array to store the coefficients. // // CONSTRUCTOR for the polynomial class // POSTCONDITION: This polynomial has been create with all zero // coefficients, the size is set to the DEFAULT_CAPACITY, and the // current degree is set to 0. // MODIFICATION MEMBER FUNCTIONS for the polynomial class // void assign_coef(double coefficient, unsigned int exponent) // POSTCONDITION: Sets the coefficient for the specified exponent. // // void reserve(unsigned int number) // POSTCONDITION: The size of the array for coefficients has been changed // to the requested number (but not less that the size needed to store the // current non-zero coefficients). In effect, this guarantees that member // functions will not need to allocate new memory for exponents through // CONSTANT MEMBER FUNCTIONS for the polynomial class // double coefficient(unsigned int exponent) const // POSTCONDITION: Returns coefficient at specified exponent of this // polynomial. // // double getsize( ) const // POSTCONDITION: Returns size of the coef array // // unsigned int degree( ) const // POSTCONDITION: The function returns the value of the largest exponent // with a non-zero coefficient. // If all coefficients are zero, then the function returns zero. // // double eval(double x) const; // POSTCONDITION: Evaluates a polynomial with a given value of x. // For example, given the polynomial 3x^2-7x and a value of x to be 2, // the result would be 3 * (2^2) - 7 * (2^1) = 3 * 4 - 7 * 2 = 12 - 14 = -2 // NON-MEMBER BINARY OPERATORS for the polynomial Class // polynomial operator -(const polynomial& p1, const polynomial& p2) // POSTCONDITION: return-value is a polynomial with each coefficient // equal to the difference of the coefficients of p1 & p2 for any given // exponent. // // polynomial operator +(const polynomial& p1, const polynomial& p2) // POSTCONDITION: return-value is a polynomial with each coefficient // equal to the sum of the coefficients of p1 & p2 for any given // exponent. // // NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class // ostream& operator << (ostream& out, const polynomial& p) // POSTCONDITION: The polynomial has been printed to ostream out, which, // in turn, has been returned to the calling function. // // DYNAMIC MEMORY // Since this class uses dynamic memory, the copy constructor and assignment // operator are written, and there is a destructor implemented. #ifndef POLY_H #define POLY_H #include #include using namespace std; const unsigned int DEFAULT_CAPACITY = 10; typedef size_t size_type; typedef double value_type; class polynomial { public: // CONSTRUCTORS and DESTRUCTOR polynomial(); polynomial(const polynomial& source); ~polynomial( ); // MODIFICATION MEMBER FUNCTIONS void operator =(const polynomial& source); void assign_coef(double coefficient, unsigned int exponent); void reserve(unsigned int number); void setcurrent_degree(int init_degree); void setsize(int init_size); // CONSTANT MEMBER FUNCTIONS double coefficient(unsigned int exponent) const; unsigned int degree( ) const; double eval(double x) const; unsigned int getsize ( ) const; private: double *coef; // Pointer to the dynamic array unsigned int size; // Current size of the dynamic array unsigned int current_degree; // Current degree of the polynomial }; // NON-MEMBER FUNCTIONS ostream& operator << (ostream& cout, const polynomial& r); polynomial operator +(const polynomial& l, const polynomial& r); polynomial operator -(const polynomial& l, const polynomial& r); #endif ``````

Topic archived. No new replies allowed.