Adding Polynomials problem

Hi guys,

I have been working on an assignment we have at the University. Basically , you have 2 classes:

class Polynomial and class Term.

The Polynomial class have 2 private data members ,

- Term *polynomial;
- int numofTerms;

The Term class have 2 private data members ,

- double coefficient;
- int power;

Term *polynomial is a dynamic array that stores Term objects.

I have all the necessary constructors to create Polynomials , as well as copy constructor and other constructors to create polynomials by giving a term as a parameter.

Furthermore , I overload the operator+= so that it can add two Polynomials objects together ,



p1 = x^2 //pseudo code.
p2 = 4x^2+2x^6 //pseudo code.

p1+=p2;

thus , the answer should be

e.g. (x^2+4x^2+2x^6)

this should be stored in p1's polynomial private member.

the problem is that I have to have the array in the simplest forms , because polynomials with the same power can be added together AND the array should be in the form of smallest power to largest power (degree). I can't figure out how to sort the array and also add the terms with the same power together , because the array would have to shrink if a term with the same power gets added. From here I think the array would have to be stored into a temp array with the larger value , but this is where I get a little confused.

Please try to help me figure out this one part of the program.
I can also post examples of my code if needed.

Thanks for sharing interest.
Last edited on
Look at the STL reference on this site (http://www.cplusplus.com/reference/stl/ ). There are two types of containers that keep their elements sorted automatically. One of them is better suited for this problem than the other.
Topic archived. No new replies allowed.