I would insert each term in terms in ascending order based on the exponent so that the exp term with exponent 0 has index 0 in terms, etc. I would expand terms if the exponent of a term to be added to terms is above value of nterms. The default coefficient of each term in terms would be zero. Each term inserted into terms would appropriately adjust the value of the coeffient of the term with the same exponent.
To write in standard polynomial form with largest exponent first I would then start at the end of terms and loop backward. Each term could be output only in the value of the coefficient wasn't zero. The display format would be coefficient followed by variable followed by ^ sign followed by exponent without spaces between each of the values in the display.
To add two terms I would determine the polynomial with the smallest value of nterms and expand it to the same number of nterms as the larger one. Looping through each term in the polynomials and adding ( or subtracting as the case may be) the coefficients of terms with the same exponent will result in the coefficient of the term a third polynomial representing the sum.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Offline 2,253 posts
since Jul 2005