"9x^10+54x^7 + 3x^6+ 4x^7+2x^6" for example.

Now i want to add the like terms but i am not able to do that.

Can someone help me with this please.

Here is my code:

/* Multiplying two polynomials*/ #include <stdio.h> #define MAX_TERMS 101 typedef struct { int coef; int exp; } POLY_TERM; /* multiplying two polynomials a and b, with output polynomial c.*/ void polyMult(POLY_TERM a[], POLY_TERM b[], POLY_TERM c[]){ int aIndex=0, bIndex=0, cIndex=0; for (aIndex=0; aIndex<MAX_TERMS; aIndex++){ //while ((a[aIndex].exp>=0) && (b[bIndex].exp>=0)){ if(a[aIndex].exp==-1) break; for (bIndex=0; bIndex<MAX_TERMS; bIndex++){ if(b[bIndex].exp==-1) break; c[cIndex].coef =a[aIndex].coef * b[bIndex].coef; c[cIndex].exp = a[aIndex].exp + b[bIndex].exp; cIndex++;// bIndex++; //aIndex++; } } /*Adding like terms*/ for (cIndex=0; cIndex<MAX_TERMS; cIndex++){ if (c[cIndex].exp==c[cIndex+1].exp) c[cIndex].coef+=c[cIndex+1].coef; c[cIndex].exp= c[cIndex].exp; } } /* Print a polynomial. */ void polyPrint(POLY_TERM p[]){ int i=0; printf("%dx^%d", p[i].coef, p[i].exp); i++; while (p[i].exp>=0){ printf(" + %dx^%d", p[i].coef, p[i].exp); i++; } printf("\n"); } int main(int argc, char **argv){ int i; POLY_TERM a[MAX_TERMS], b[MAX_TERMS], c[MAX_TERMS]; /* Create polynomial a */ for (i=0; i<MAX_TERMS; i++) a[i].exp=-1; a[0].coef=3; a[0].exp=5; a[1].coef=18; a[1].exp=2; a[2].coef=-3; a[2].exp=1; /* Create polynomial b */ for (i=0; i<MAX_TERMS; i++) b[i].exp=-1; b[0].coef=3; b[0].exp=5; b[1].coef=18; b[1].exp=2; b[2].coef=-3; b[2].exp=1; b[3].coef=2; b[3].exp=1; /*Do polynomial Multiplication*/ polyPrint(a); polyPrint(b); for (i=0; i<MAX_TERMS; i++) c[i].exp=-1; polyMult(a, b, c); polyPrint(c); return(0); }

Can someone help me check my code i see why am not able to do it. If there is a better way in C, i most welcome the code.

Thanks