Wednesday 22 August 2012

Multiplication of two(2) polynomials | How to write code

Data structure program | multiplication of polynomials | source code
/* Program for multiplication of polynomials */

#include<stdio.h>
#include<conio.h>
#define max 20

typedef struct
{
int coef;
int deg;
}poly;
poly p1[max],p2[max],p3[max],p4[max];

void createpoly(poly p[],int x)
{
int i;
for(i=0;i<x;i++)
{
printf("\nEnter the %d coefficient of polynomial:- ",i+1);
scanf("%d",&p[i].coef);
printf("\nEnter the %d degree of polynomial:- ",i+1);
scanf("%d",&p[i].deg);
}
sort(p,x);
printf("\nEntered polynomial is:- ");
displaypoly(p,x);
}

int displaypoly(poly p[],int x)
{
int i;
for(i=0;i<x-1;i++)
printf("%dX^%d+",p[i].coef,p[i].deg);
printf("%dX^%d",p[i].coef,p[i].deg);
return 0;
}

int sort(poly p[],int x)
{
int i,j,temp1,temp2;
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(p[i].deg<p[j].deg)
{
temp1=p[i].deg;
p[i].deg=p[j].deg;
p[j].deg=temp1;
temp2=p[i].coef;
p[i].coef=p[j].coef;
p[j].coef=temp2;
}
}
}
return 0;
}

void mulpoly(int m,int n)
{
int i=0,j=0,k=0,l=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++,k++)
{
p3[k].coef=p1[i].coef*p2[j].coef;
p3[k].deg=p1[i].deg+p2[j].deg;
}
}
sort(p3,k);
for(i=0,j=i+1,l=0;i<k;i++,j++,l++)
{
if(p3[i].deg==p3[j].deg)
{
p4[l].coef=p3[i].coef+p3[j].coef;
p4[l].deg=p3[i++].deg;
}
else
{
p4[l].coef=p3[i].coef;
p4[l].deg=p3[i].deg;
}
}
displaypoly(p4,l);
}

void main()
{
int m,n;
clrscr();
printf("\nHow many term you want into the 1st polynomial:- ");
scanf("%d",&m);
createpoly(p1,m);
printf("\n\nHow many term you want into the 2nd polynomial:- ");
scanf("%d",&n);
createpoly(p2,n);
printf("\n\nMultiplication of two polynomials is:- ");
mulpoly(m,n);
getch();
}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: