Sparse Polynomial representation and addition
Program to represent two polynomials using arrays and compute their sum
/* Representation of Polynomials using arrays */
/* Addition of two Polynomials */
#include <stdio.h>
#define MAX 10
int main(){
int poly1[MAX]={0},poly2[MAX]={0},poly3[MAX]={0};
int i,deg1,deg2,deg3;
printf(“nEnter degree of first polynomial?”);
scanf(“%d”,°1);
printf(“nEnter degree of second polynomial?”);
scanf(“%d”,°2);
printf(“nFor first polynomial:”);
for(i=0;i<deg1;i++){
printf(“nEnter Coefficient for Exponent %d> “,i);
scanf(“%d”,&poly1[i]);
}
printf(“nFor second polynomial:”);
for(i=0;i<deg2;i++){
printf(“nEnter Coefficient for Exponent %d> “,i);
scanf(“%d”,&poly2[i]);
}
printf(“nGenerating sum…!”);
printf(“nPress any key to continue…”);
deg3 = (deg1>deg2)?deg1:deg2;
for(i=0;i<deg3;i++)
poly3[i] = poly1[i] + poly2[i];
printf(“nnAddition Result:nn”);
for(i=deg1-1;i>=0;i–)
printf(“(%dx^%d)+”,poly1[i],i);
printf(“b n”);
for(i=deg2-1;i>=0;i–)
printf(“(%dx^%d)+”,poly2[i],i);
printf(“b n”);
for(i=deg3-1;i>=0;i–)
printf(“(%dx^%d)+”,poly3[i],i);
printf(“b n”);
}