DATA STRUCTURES IN C polynomials to read multiply and display

Data structures in C in this subject we will come across the program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define size 20;
type def struct poly
{
int coef;
int exp;
}poly;
void main()
{
int t1,t2,t3;
poly p1[size],p2[size],p3[size];
int read(poly p1[size]);
int mult(int t1,int t2,poly p[size],poly p2[size],poly p3[size]);
void display(poly p1[size],int t2);
printf("\n Enter the first polynomial");
t1=read(p1);
printf("\n enter the second polynomial\n");
t2=read(p2);
t3=mult(t1,t2,p1,p2,p3);
printf("\n the multiplication of 2 polynomial is \n");
display(p3,t3);
getch();
clrscr();
}
int read(poly p[])
{
int term,n;
printf("\n how many number of terms are there?");
scanf("%d",&n);
if(n>size)
{
printf("\n invalid number of terms");
getch();
exit(1);
}
printf("\n enter the terms of the polynomial");
printf("in descending order of exponent\n");
for(term=0;term<n;term++)
{
printf("enter the coefficient and exponent");
scanf("%d%d",&p[term].coef,&p[term].expo);
}
return(n);
}
void display(poly p[],int n)
{
int term;
for(term=0;term<n;term++)
{
if(p[term].coef<0)
printf("%dx^%d",p[term].coef,p[term].expo);
else
{
if(term==0)
printf("%dx^%d",p[term].coef,p[term].expo);
}
}
}
int multi(int n1,int n2,poly p1[], poly p2[],poly p2[])
{
int i,j,k,nxt3,temp;
int ex;
int co;
i=nxt3=0;
while(i<n1)
{
j=0;
while(j<n2)
{
ex=p1[i].expo+p2[j].expo;
co=p1[i].coef*p2[j].coef;
k=0;
temp=0;
while(k<nxt3&&temp)
{
if(p3[k],expo==ex)
{
temp=1;
break;
}
else
k++;
}
if(temp==1)
p3[k].coef=p3[k].coef+co;
else
{
p3[nxt3].expo=ex;
p3[nxt3].coef=co;
nxt3++;
}
j++;
}
i++;
}
return(nxt3);
}

Comments

Popular posts from this blog

Visti Cloud Service (VCS)

Being a DARK SKINNED is not a PROBLEM