October 21, 2015

Polynomial program in c using structure

polynomial program in c, c program for polynomial addition, how to write a polynomial program in c language, polynomial c programming program, polynomial programming of c language 




Polynomial program in c using structure

#include<stdio.h>
#define MAX 10
struct polynomial
{
int degree;
int coef[MAX];
} poly[MAX],add;

void CreatePoly(int);
void DisplayPoly(int);
void AddPoly(int,int);
void init(int); //Initialize all coefficients to 0

int main()
{
int n;
printf("\nProgram to add polynomials\n");
printf("\nEnter the number of polynomials : ");
scanf("%d",&n);
CreatePoly(n);
return(0);
}
void CreatePoly(int n)
{
int i,j,maxDeg=0,deg,temp;
init(n);
for(i=0;i<n;i++)
{
printf("\nEnter the Degree of Polynomial %d : ",i+1);
scanf("%d",&poly[i].degree);
if(poly[i].degree>maxDeg)
maxDeg=poly[i].degree;
printf("\nEnter the Coefficients of Polynomial of degree %d\n",poly[i].degree);
deg=MAX-poly[i].degree;
temp=poly[i].degree;
for(j=deg-1;j<MAX;j++)
{
printf("\nCoefficient of  x^%d : ",temp--);
scanf("%d",&poly[i].coef[j]);
}
}
DisplayPoly(n);
AddPoly(n,maxDeg);
}
void DisplayPoly(int n)
{
int i,j,deg,temp;
printf("\nThe Polynomials are\n");
for(i=0;i<n;i++)
{
deg=MAX-poly[i].degree;
temp=poly[i].degree;
printf("Polynomial %d : ",i+1);
for(j=deg-1;j<MAX-1;j++)
printf("%+dx^%d ",poly[i].coef[j],temp--);
printf("%+d",poly[i].coef[j]);
printf("\n");
}
}
void init(int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<MAX;j++)
poly[i].coef[j]=0;
}
for(j=0;j<MAX;j++)
add.coef[j]=0;
}
void AddPoly(int n,int maxDeg)
{
int i,j,deg;
deg=MAX-maxDeg;
for(i=0;i<n;i++)
{
for(j=deg-1;j<MAX;j++)
add.coef[j]+=poly[i].coef[j];
}
printf("\nThe addition of the %d polynomials is\n",n);
for(j=deg-1;j<MAX-1;j++)
printf("%+dx^%d ",add.coef[j],maxDeg--);
printf("%+d",add.coef[j]);
printf("\n");
}

No comments:
Write comments