Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 1 · syllabusLesson live

Polynomial representation using arrays

Store a polynomial as (coefficient, exponent) pairs and add two of them in one merge-like pass.

Syllabus says: “Polynomial representation using Arrays”

On this page · 12 sections
ormakkurippu ✎
Never-forget card

Compare exponents: BIGGER goes first, EQUAL gets added, ZERO sum gets dropped.

Picture it

Two queues of students sorted by height merging at a gate: the taller one goes in first; twins (same height) walk in together as one.

Write this in the exam

A polynomial is stored as an array of (coef, expon) terms in decreasing order of exponent; addition merges the two term lists in O(m + n).

What you will be able to do

  • Represent a polynomial with arrays (two methods).
  • Write the polynomial addition algorithm.
  • Explain why the term representation suits sparse polynomials.

The story

A polynomial like 5x⁴ + 3x² + 7 is just a list of terms. Each term has a coefficient and an exponent. To add two polynomials by hand you line up equal powers and add their coefficients — the program does exactly the same, but it must store the terms cleverly first.

Two ways to store a polynomial

Method 1 — coefficient array indexed by exponent

7
x⁰
0
x¹
3
x²
0
x³
5
x⁴
5x⁴ + 3x² + 7 stored as coef[i] = coefficient of xⁱ. Simple, but x¹⁰⁰⁰ + 1 would need 1001 slots.

Method 2 — array of terms (coef, expon), decreasing exponent

coefexpon
054
132
270
Only non-zero terms are stored. Horowitz keeps all polynomials in one global terms[] array with start and finish indices for each.
C
#define MAX_TERMS 100
typedef struct { float coef; int expon; } term;
term terms[MAX_TERMS];   /* shared storage for all polynomials */
int avail = 0;            /* next free slot */

Worked example

A(x) = 5x⁴ + 3x² + 7 and B(x) = 2x⁴ + 6x³ − 3x² + 4.

CompareActionC so far
x⁴ vs x⁴ (equal)5 + 2 = 7 → output 7x⁴7x⁴
x² vs x³3 > 2 → output B's 6x³7x⁴ + 6x³
x² vs x² (equal)3 + (−3) = 0 → drop the term!7x⁴ + 6x³
x⁰ vs x⁰ (equal)7 + 4 = 11 → output 117x⁴ + 6x³ + 11
Watch out

Forgetting to drop zero-coefficient terms is the most common mistake here; the answer would wrongly contain 0x².

Now watch it run

Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶.

Loading visualizer…

Algorithm: add two polynomials

AlgorithmPolynomial Addition using Array (Method 1: coefficient array)
  1. Step 1:
    Start.
  2. Step 2:
    Read the degree d1 of the first polynomial.
  3. Step 3:
    Read its coefficients into p1[0..d1], from the lowest power to the highest (p1[i] is the coefficient of x^i).
  4. Step 4:
    Read the degree d2 of the second polynomial.
  5. Step 5:
    Read its coefficients into p2[0..d2] in the same way.
  6. Step 6:
    Set max = maximum(d1, d2).
  7. Step 7:
    Repeat for i = 0 to max:
    • If i <= d1, set sum[i] = p1[i]; otherwise set sum[i] = 0.
    • If i <= d2, set sum[i] = sum[i] + p2[i].
  8. Step 8:
    Display the sum from the highest power to the lowest: for i = max down to 0, display sum[i] x^i.
  9. Step 9:
    Stop.
AlgorithmPolynomial Addition using Array (Method 2: array of terms, used in the C program)
  1. Step 1:
    Start.
  2. Step 2:
    Read n1 and the coefficient and exponent of each term of the first polynomial into p1[], in descending order of exponent.
  3. Step 3:
    Read n2 and the terms of the second polynomial into p2[] in the same way.
  4. Step 4:
    Set i = 0, j = 0 and k = 0.
  5. Step 5:
    Repeat while i < n1 and j < n2:
    • If p1[i].exp > p2[j].exp, copy p1[i] to sum[k]; increment i and k.
    • Else if p1[i].exp < p2[j].exp, copy p2[j] to sum[k]; increment j and k.
    • Else (equal exponents): if p1[i].coeff + p2[j].coeff is not 0, store this sum with exponent p1[i].exp in sum[k] and increment k. Then increment i and j.
  6. Step 6:
    While i < n1, copy p1[i] to sum[k]; increment i and k.
  7. Step 7:
    While j < n2, copy p2[j] to sum[k]; increment j and k.
  8. Step 8:
    Display the k terms of sum[] as coeff x^exp (display 0 if k = 0).
  9. Step 9:
    Stop.

C implementation

polynomial_add_array.ctested ✓ 220/220 cases
C
/* Polynomial Addition using Arrays of terms — KTU PCCST303, Module 1 (lab-record style)
 * Enter each term as: coefficient exponent, in descending order of exponent. */
#include <stdio.h>

struct Term {
    int coeff, exp;
};

int main()
{
    struct Term p1[20], p2[20], sum[40];
    int n1, n2, i, j, k;

    printf("Enter number of terms in first polynomial: ");
    scanf("%d", &n1);
    printf("Enter coefficient and exponent of each term (descending order of exponent):\n");
    for (i = 0; i < n1; i++)
        scanf("%d %d", &p1[i].coeff, &p1[i].exp);
    printf("Enter number of terms in second polynomial: ");
    scanf("%d", &n2);
    printf("Enter coefficient and exponent of each term (descending order of exponent):\n");
    for (i = 0; i < n2; i++)
        scanf("%d %d", &p2[i].coeff, &p2[i].exp);

    i = 0;
    j = 0;
    k = 0;
    while (i < n1 && j < n2) {
        if (p1[i].exp > p2[j].exp) {
            sum[k] = p1[i];
            i++;
            k++;
        }
        else if (p1[i].exp < p2[j].exp) {
            sum[k] = p2[j];
            j++;
            k++;
        }
        else {                             /* equal exponents: add */
            if (p1[i].coeff + p2[j].coeff != 0) {
                sum[k].coeff = p1[i].coeff + p2[j].coeff;
                sum[k].exp = p1[i].exp;
                k++;
            }
            i++;
            j++;
        }
    }
    while (i < n1) {                       /* remaining terms of p1 */
        sum[k] = p1[i];
        i++;
        k++;
    }
    while (j < n2) {                       /* remaining terms of p2 */
        sum[k] = p2[j];
        j++;
        k++;
    }

    printf("Sum = ");
    if (k == 0)
        printf("0");
    for (i = 0; i < k; i++) {
        if (i == 0)
            printf("%dx^%d", sum[i].coeff, sum[i].exp);
        else if (sum[i].coeff < 0)
            printf(" - %dx^%d", -sum[i].coeff, sum[i].exp);
        else
            printf(" + %dx^%d", sum[i].coeff, sum[i].exp);
    }
    printf("\n");
    return 0;
}
Sample input / output · model Q12a · real run, compiled with gcc
Enter number of terms in first polynomial: 4
Enter coefficient and exponent of each term (descending order of exponent):
2 3
5 2
-4 1
5 0
Enter number of terms in second polynomial: 4
Enter coefficient and exponent of each term (descending order of exponent):
4 3
2 2
3 1
-6 0
Sum = 6x^3 + 7x^2 - 1x^1 - 1x^0

Bold = what you type. Everything else is printed by the program.

Open in the Code Lab →

Complexity

O(m + n) time: each iteration consumes at least one term. Space O(m + n) for the result. Method 1 addition is O(max degree), which is terrible for sparse polynomials.

Quick check — can you get them all?

Question 1

Q1.Why not store x¹⁰⁰⁰ + 1 in an array indexed by exponent?

Question 2

Q2.While adding, A has exponent 3 and B has exponent 5. What happens?

Question 3

Q3.Adding polynomials with m and n terms takes…

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

Exam-ready summary

  • Store non-zero terms as (coef, expon) in decreasing exponent order.
  • Add: bigger exponent first; equal → add coefficients; skip a zero sum.
  • Copy leftovers. O(m + n).

References

  • Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §2.4 (Polynomials)

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)