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
1
2
3
4
5
6
7
8
9
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
1
2
3
4
5
6
7
8
9

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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 <iostream>
#include <cstdlib>

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.