Manipulating polynomials

Hello,

I'm having trouble manipulating polynomials using dynamic arrays and pointers. My code compiles and outputs all of the prompts, but the answers it gives are incorrect. I thought I could get this figured out and finished but I can't seem to get it and the assignment is due in less than 4 hours from now.
Any help would be greatly appreciated. Thanks!

For this assignment, you will write a C++ class which will represent a polynomial. Then
operations will be performed on polynomials and numbers. You will write functions to add,
subtract, and multiply two polynomials or a polynomial and a number.

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  #include <cmath>
#include <iostream>
using namespace std;

class polynomial
{
public:
	friend ostream& operator<<(ostream& outfile, const polynomial&);
	friend polynomial operator+(int, const polynomial&);
	friend polynomial operator*(int, const polynomial&);
	friend polynomial operator-(int, const polynomial&);

	polynomial();
	polynomial(const polynomial&);
	polynomial(int*, int);
	~polynomial();

	polynomial operator*(const polynomial&);
	polynomial operator*(int);
	polynomial operator+(const polynomial&);
	polynomial operator+(int);
	
	void operator=(const polynomial&);
	void operator=(int);
	
	polynomial operator-();
	
	polynomial operator-(const polynomial&);
	polynomial operator-(int);

private:
	int *expr;
	int degree;
};


int main()
{
	int poly1[] = {-2,4,6};
	int poly2[] = {1,3,-5};
	int poly3[] = {3, 0, 4, 0, 5};
	
	polynomial p1(poly1, 2);
	polynomial p2(poly2, 2);
	polynomial p3(poly3, 4);
	polynomial result;


	cout << "p1 = " << p1 << endl;
	cout << "p2 = " << p2 << endl;
	cout << "p3 = " << p3 << endl << endl;

	cout << endl;
	result = p1 * p2;
	cout << "p1 * p2 = " << result << endl;

	result = p2 + p1;
	cout << "p2 + p1 = " << result << endl;

	result = p1 + p3;
	cout << "p1 + p3 = " << result << endl;

	result = p1 * p3;
	cout << "p1 * p3 = " << result << endl;

	result = p1 + 5;
	cout << "p1 + 5 = " << result << endl;

	result = p2 * 6;
	cout << "p2 * 6 = " << result << endl;

	result = 4 + p3;
	cout << "4 + p3 = " << result << endl;

	result = 3 * p1;
	cout << "3 * p1 = " << result << endl;

	result = 5 - p2;
	cout << "5 - p2 = " << result << endl;

	result = 10;
	cout << "result = 10: " << result << endl << endl;

	return 0;
}

ostream& operator<<(ostream& outfile, const polynomial& p) //display the polynomial
{
	for (int i = p.degree; i >= 0; i--)
	{
		if (i == p.degree && p.expr[i] < 0)
			outfile << "-";
		else if (i != p.degree)
		{
			if (p.expr[i] > 0)
				outfile << " + ";
			if (p.expr[i] < 0)
				outfile << " - ";
		}
		if (p.expr[i] != 0)
		{
			outfile << abs(p.expr[i]);
			if (i > 0)
				outfile << "x";
			if (i > 1)
				outfile << "^" << i;
		}
	}
	return outfile;
}

polynomial operator+(int n, const polynomial& p) //add an int to the copy
{
	polynomial temp;
	
	temp.expr = new int [p.degree];
	temp.degree = p.degree;
	
	*temp.expr = p.expr[0] + n;
	return temp;
}

polynomial operator*(int n, const polynomial& p) //multiply the copy by the int
{
	polynomial temp;
	
	temp.expr = new int [p.degree];
	temp.degree = p.degree;
	
	for (int i=0; i< p.degree; i++)
	{
	temp.expr[i] = n * p.expr[i];
	}
	return temp;
}

polynomial operator-(int n, const polynomial& p) //subtract int from copy if int is first
{
	polynomial temp;
	
	temp.expr = new int [p.degree];
	temp.degree = p.degree;
	
	*temp.expr = p.expr[0] - n;
	return temp;
}

polynomial::polynomial() //cnstrctr, set priv int degree to 0, set priv expr pointer to NULL
{ 
	degree = 0;
		
	expr = NULL;
	
}

polynomial::polynomial(const polynomial& p) //copy constructor
{
	degree = p.degree;
	expr = new int [degree];
	
	for (int i=0; i<degree; i++)
		expr[i] = p.expr[i];
}

polynomial::polynomial(int *poly_expr, int d) //cnstrctr, takes int pointer and int value.
                                              //sets degree to the int value, and the int
                                              //pointer will be a dynamic array so a deep
                                              //copy of that to expr will be done
{
	degree = d;
	
	expr = new int [degree];
	
	for (int i=0; i < degree+1; i++)
	{
	expr[i] = poly_expr[i];
	}
	
}

polynomial::~polynomial() //deallocate the array that expr is pointing to
{
	delete [] expr;
}

polynomial polynomial::operator*(const polynomial& p) //multiply poly by poly
{
	polynomial temp;
	
	if (p.degree > degree)
	{
		temp.expr = new int [p.degree];
		temp.degree = p.degree;
	}
	else
	{
		temp.expr = new int [degree];
		temp.degree = degree;
	}
	
	for (int i=0; i < degree; i++)
	{
		for (int j=0; j < temp.degree; j++)
			temp.expr[i+j] += expr[i] * p.expr[j];
	}
	return temp;
	
}

polynomial polynomial::operator*(int n) //multiply poly by int
{
	polynomial temp;
	
	temp.expr = new int [degree];
	temp.degree = degree;
	
	for (int i=0; i < degree; i++)
		{
		temp.expr[i] = expr[i] * n;
		}
	return temp;
}

polynomial polynomial::operator+(const polynomial& p) //add invoking object's poly to poly
{
	polynomial temp;
	
	if (p.degree > degree)
	{
		temp.expr = new int [p.degree];
		temp.degree = p.degree;
		
		for (int i=0; i < p.degree; i++)
	{
		temp.expr[i] = expr[i] + p.expr[i];
	}
	
	}
	else
	{
		temp.expr = new int [degree];
		temp.degree = degree;
		
		for (int i=0; i < degree; i++)
	{
		temp.expr[i] = expr[i] + p.expr[i];
	}
	
	}
	
	return temp;
	
}

polynomial polynomial::operator+(int n) //add invoking object's poly to int
{
	polynomial temp;
	
	temp.expr = new int [degree];
	temp.degree = degree;
	
	temp.expr[0] = n + expr[0];
	
	return temp;
}

void polynomial::operator=(const polynomial& p) //assignment operator to perform a copy
{
	if (this != &p)
	{
		delete [] expr;
		degree = p.degree;
		expr = new int [degree];
		
		for (int i=0; i < degree; i++)
			expr[i] = p.expr[i];
		}
}

void polynomial::operator=(int n) //polynomial of degree 0 is created with 1 coefficient
{
	*expr = n;
}

polynomial polynomial::operator-() //make the polynomial a negative
{
	polynomial temp;
	
	for (int i= 0; i < degree+1; i++)
	{
	temp.expr[i] = expr[i] * -1;
	}
	
	return temp;
}

polynomial polynomial::operator-(const polynomial& p)//subtract invoking object's polynomial
													 //from another polynomial
{
	polynomial temp;
	
	if (p.degree > degree)
	{
		temp.expr = new int [p.degree];
		temp.degree = p.degree;
		
		for (int i=0; i < p.degree; i++)
	{
		temp.expr[i] = expr[i] - p.expr[i];
	}
	
	}
	else
	{
		temp.expr = new int [degree];
		temp.degree = degree;
		
		for (int i=0; i < degree; i++)
	{
		temp.expr[i] = expr[i] - p.expr[i];
	}
	
	}
	
	return temp;

}

polynomial polynomial::operator-(int n) //subtract an invoking object's polynomial from int
{
	polynomial temp;
	
	temp.expr = new int [degree];
	temp.degree = degree;
	
	*temp.expr = n - expr[0];
	return temp;
}


This is what the output should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
p1 = 6x^2 + 4x - 2
p2 = -5x^2 + 3x + 1
p3 = 5x^4 + 4x^2 + 3


p1 * p2 = -30x^4 - 2x^3 + 28x^2 - 2x - 2
p2 + p1 = 1x^2 + 7x - 1
p1 + p3 = 5x^4 + 10x^2 + 4x + 1
p1 * p3 = 30x^6 + 20x^5 + 14x^4 + 16x^3 + 10x^2 + 12x - 6
p1 + 5 = 6x^2 + 4x + 3
p2 * 6 = -30x^2 + 18x + 6
4 + p3 = 5x^4 + 4x^2 + 7
3 * p1 = 18x^2 + 12x - 6
5 - p2 = 5x^2 - 3x + 4
result = 10: 10


This is the output my code is giving:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
p1 = 6x^2 + 4x - 2
p2 = -5x^2 + 3x + 1
p3 = 5x^4 + 4x^2 + 3


p1 * p2 =  - 2x - 2
p2 + p1 =  + 7x - 1
p1 + p3 =  + 17x^3 + 10x^2 + 4x + 1
p1 * p3 =  + 33x^3 + 2x^2 + 16x - 6
p1 + 5 = 12x^2 + 7x + 3
p2 * 6 = 12x^2 + 18x + 6
4 + p3 =  + 33x^3 + 2x^2 + 16x + 7
3 * p1 =  + 12x - 6
5 - p2 =  + 12x - 4
result = 10:  + 12x + 10


Thanks again for any help.
Hi,

Without looking at any of this, by far the easiest thing to do is use a debugger.
Decide where in your code you print the value that is wrong, use the debugger to see what the values of your variables are leading up to that point. Thus deduce what & where it went wrong.

The ones that are built into IDE's are hopefully easy to use. You can put in breakpoints and have a watchlist of variable values, and step through the code 1 line at a time.


Otherwise you could spend days staring at code or putting output statements everywhere, and still might not get it fixed.

Hope all goes well
Thanks for the suggestion, I'm using ubuntu and coding with geany. Does that have a debugger in it? I tried looking in the ubuntu software center and the only c++ debugger listed is something called nimiver but I don't have a clue how to use it :/
Hi,

I haven't used Geany Is it an IDE with menus etc , or is it a text editor that is C++ aware?

Which compiler are you using, because there are cmd line debuggers such gdb which works with gcc. There is a tutorial on this site on how to use it quickly. It should already (hopefully) be installed.

Depending on your installation, you may have some dev tools already installed, if not you might just have enough time to install an IDE such QtCreator, CodeBlocks, Eclipse, or even KDevelop. Then copy all your code into it, and finally use the debugger.
Topic archived. No new replies allowed.