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
Compare exponents: BIGGER goes first, EQUAL gets added, ZERO sum gets dropped.
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.
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
Method 2 — array of terms (coef, expon), decreasing exponent
| coef | expon | |
|---|---|---|
| 0 | 5 | 4 |
| 1 | 3 | 2 |
| 2 | 7 | 0 |
#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.
| Compare | Action | C 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 11 | 7x⁴ + 6x³ + 11 |
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 ◀ ▶. Keys: Space play/pause, ← → step.
Algorithm: add two polynomials
- Step 1:Start.
- Step 2:Read the degree d1 of the first polynomial.
- Step 3:Read its coefficients into p1[0..d1], from the lowest power to the highest (p1[i] is the coefficient of x^i).
- Step 4:Read the degree d2 of the second polynomial.
- Step 5:Read its coefficients into p2[0..d2] in the same way.
- Step 6:Set max = maximum(d1, d2).
- 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].
- Step 8:Display the sum from the highest power to the lowest: for i = max down to 0, display sum[i] x^i.
- Step 9:Stop.
- Step 1:Start.
- Step 2:Read n1 and the coefficient and exponent of each term of the first polynomial into p1[], in descending order of exponent.
- Step 3:Read n2 and the terms of the second polynomial into p2[] in the same way.
- Step 4:Set i = 0, j = 0 and k = 0.
- 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.
- Step 6:While i < n1, copy p1[i] to sum[k]; increment i and k.
- Step 7:While j < n2, copy p2[j] to sum[k]; increment j and k.
- Step 8:Display the k terms of sum[] as coeff x^exp (display 0 if k = 0).
- Step 9:Stop.
C implementation
polynomial_add_array.ctested ✓ 220/220 cases/* 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.
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?
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)