P(x) = a0 + a1x^1 + a2x^2 + · · · + anx6(n) (note that a(n) is != 0)
value n is called the degree of the polynomial and is stored in the data member degree.
The coefficients ai of the polynomial are stored in the dynamically allocated table coeff of size
degree+1 with ai stored in coeff[i]
// file polynomial.h
#ifndef MY_POLY
#define MY_POLY
#include <iostream>
usingnamespace std;
class polynomial
{
// Overloading of operator << for the printing of a polynomial.
// See example of execution.
friend ostream& operator<< (ostream&, const polynomial&);
public:
// Default constructor. Produces the polynomial P(x) = 0.
// (the degree is 0 and coeff[0] must be set to 0)
polynomial ();
// Produces a polynomial of degree n whose coefficients are found in
// c’s pointee. To be consistent with the Matlab exercise c’s pointee stores
// the polynomial coefficients from the coefficient of the term of higher
// exponent to the coefficient of the term of lower exponent.
// (Note that data member coeff uses the more convenient reverse order.)
//
// i.e. the statements "int a* = {1,2,3}; polynomial p(2,a);"
// produces the polynomial, p, that represents P(x) = x^2 + 2x + 3.
//
// Also "polynomial p(n,NULL)" should create the polynomial representation,
// p, of P(x) = x^n
polynomial (unsigned n, double * c);
// Copy constructor is required because ...
polynomial(const polynomial& p);
// If n is within range assigns value to the coefficient coeff[n]
// and returns 1. Otherwise returns 0
int setcoeff (unsigned n, double value);
// Returns coeff[n] if n is within range. Otherwise returns 0
double getcoeff (unsigned n) const;
// Returns degree.
unsigned getdegree () const;
// Returns the derivative of the polynomial.
polynomial diff () const;
// Evaluate the polynomial at point x.
double eval(constdouble x) const;
// Destructor is required because ...
~polynomial();
private:
// The degree of the polynomial.
unsigned degree;
// Pointer to the coefficients of the polynomial
double * coeff;
};
#endif
The following main
1 2 3 4 5 6 7 8 9
#include "polynomial.h"
int main() {
double a [] = {2, -3, 0, -2};
polynomial p(3,a);
cout << "polynomial P(x) = " << p << endl;
cout << "derivative of P, dP(x) = " << p.diff() << endl;
for (int i=1; i<=4; i++)
cout << "dP(" << i << ")=" << p.diff().eval(i) << endl;
}
#include <iostream>
#include <stdlib.h>
# include "polynomial.h"
usingnamespace std;
polynomial::polynomial() {
//Constructor, which initializes the array to zero and coeff array to 1
degree = 0;
coeff = newdouble[1];
coeff[0]= 0;
polynomial ::degree(unsigned n double *c){
coeff = coeff [0]
degree n;
coeff = newdouble [degree +1];
for (unsigned i = 0; i<(degree +1); i++)
coeff[i] = c [i];
degree = c
}
polynomial::polynomial(const polynomial &p){
degree = p.getdegree
}
ostream& operator <<(ostream& out, const *polynomial){
int degree = p.getdegree();
}
polynomial::polynomial(const polynomial &p){
a=p.a;
b=p.b;
cout<<a<<b;
}
// You should throw an exception if n is greater than Degree-1
// int i;
// for (i = 0; i <= degree; i++) { // copy as many as you are supposed to
// degree[i] = n[i];
// }
// for(; i < Degree; i++) { // set other values equal to zero
// size[i] = 0;
//
// polynomial(){}
////Constructor, which initializes the array to zero
//
// polynomial (int degree);
};
Polynomial::Polynomial(int *, int degree)
{
}
Tried hard but I couldnt any information tomorrow is the hand in date at 15.00 thanks
Remember the copy constructor (and operator) must copy all relevant fields. In your case, you have coeff (an array) and degree (the size of the array).
You'll need to allocate a new array of the required size (p.degree) and copy over the elements.