MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 6 of 8

Block 1 · Unit 2Arrays, Pointers and Structures10 min · Section 6 of 8

2.6Polynomials

Covers IGNOU MCS-208 §2.4

Polynomials such as 5x4+2x3+7x2+10x−85x^4 + 2x^3 + 7x^2 + 10x - 8 appear in scientific computing, graphics and error-correcting codes. To add or multiply them with a program, we first need a way to store them. This page compares two array representations and gives an efficient addition algorithm.

Two ways to store a polynomial

1. Coefficient array (dense form). Use an array c where c[e] is the coefficient of xex^e. A polynomial of degree nn needs n+1n + 1 cells.

5x^4 + 2x^3 + 7x^2 + 10x - 8 as a coefficient array
index (exponent):   0    1    2    3    4
coefficient:       -8   10    7    2    5

This is simple and fast for polynomials with few zero coefficients, but wasteful for something like x1000+1x^{1000} + 1, which would need 1001 cells for 2 terms.

2. Array of terms (sparse form). Store only the non-zero terms as (coefficient, exponent) pairs, kept in decreasing order of exponent. A structure fits naturally:

A term structure
typedef struct { int coef; int exp; } Term;
Term p[] = {{5, 4}, {2, 3}, {7, 2}, {10, 1}, {-8, 0}};
Coefficient arrayArray of terms
Cells neededdegree + 12 × (number of non-zero terms)
Good forDense polynomials (few zero coefficients)Sparse polynomials (x1000+1x^{1000} + 1)
Find coefficient of xex^eO(1)O(1): c[e]Search the terms
AdditionAdd element by elementMerge the two sorted lists

Adding two polynomials

Algorithm for C=A+BC = A + B (terms of AA and BB sorted by decreasing exponent):

  1. Look at the current term of AA and of BB.
  2. If AA‘s exponent is larger, copy AA‘s term to CC and move on in AA; if BB‘s is larger, do the same with BB.
  3. If the exponents are equal, add the coefficients; copy the sum to CC only if it is not zero; move on in both.
  4. When one polynomial is finished, copy the remaining terms of the other.

Each step moves forward in at least one of the polynomials, so the time is O(m+n)O(m + n) for mm and nn terms, and the result is already sorted.

Key takeaways

  • Dense form: c[e] = coefficient of xex^e; needs degree + 1 cells.
  • Sparse form: array of (coef, exp) structures sorted by decreasing exponent.
  • Addition by merging takes O(m+n)O(m + n) and gives a sorted result.
  • Equal exponents: add coefficients and drop the term if the sum is 0.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many cells does the coefficient-array representation of 3x50+23x^{50} + 2 need?

Show answer

Answer: D. 51

Exponents 0 to 50 need 51 cells, although only 2 are non-zero. The term representation needs just 2 pairs (4 numbers).

Question 2 · multiple choice

What is the time complexity of adding polynomials with mm and nn terms by the merge method?

Show answer

Answer: B. O(m+n)O(m + n)

Every comparison moves forward in at least one list, so there are at most m+nm + n steps. O(m⋅n)O(m \cdot n) is the cost of comparing every pair, as Program 1.2 does.

Question 3 · multiple choice

A=4x5+3x2−xA = 4x^5 + 3x^2 - x and B=2x4−3x2+6B = 2x^4 - 3x^2 + 6. What is A+BA + B?

Show answer

Answer: C. 4x5+2x4−x+64x^5 + 2x^4 - x + 6

The x2x^2 terms cancel (3−3=03 - 3 = 0) and are dropped; the other terms are copied in exponent order. Keeping "0x20x^2" breaks the non-zero-terms rule.

Question 4 · multiple choice

In the merge algorithm, the current terms are (7,3)(7, 3) from AA and (2,5)(2, 5) from BB (coefficient, exponent). What happens next?

Show answer

Answer: B. (2,5)(2, 5) is written to CC and BB moves to its next term.

The larger exponent (5) is copied first, because the result must be in decreasing order. Coefficients are added only when the exponents are equal.

Question 5 · multiple choice

Why must the terms be sorted by exponent for the merge method?

Show answer

Answer: D. Because only then do equal exponents meet at the fronts of the two lists at the same time

Sorting guarantees that when we pass an exponent in one list, it cannot appear later in that list, so one pass is enough. Memory use is the same either way.

Question 6 · short answer

Write the algorithm to add two polynomials stored as arrays of (coefficient, exponent) terms in decreasing exponent order, and trace it on A=3x4+2xA = 3x^4 + 2x and B=5x4−2x+1B = 5x^4 - 2x + 1.

Show worked answer

Let ii, jj, kk index AA, BB, CC, all starting at 0. While i<mi < m and j<nj < n: if A[i].exp>B[j].expA[i].exp > B[j].exp, copy A[i]A[i] to C[k]C[k] and increase ii, kk; else if A[i].exp<B[j].expA[i].exp < B[j].exp, copy B[j]B[j] and increase jj, kk; else let s=A[i].coef+B[j].coefs = A[i].coef + B[j].coef, and if s≠0s \ne 0 store (s,A[i].exp)(s, A[i].exp) in C[k]C[k] and increase kk; increase ii and jj. Then copy any remaining terms of AA, then of BB. Trace: (3,4)(3,4) vs (5,4)(5,4) → equal, sum 8 → C=[(8,4)]C = [(8,4)]. (2,1)(2,1) vs (−2,1)(-2,1) → equal, sum 0 → nothing stored. AA is finished; copy (1,0)(1,0) from BB. Result: 8x4+18x^4 + 1. Time O(m+n)O(m + n).

↑ ↓ move↵ openTab switch type