C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Polynomial Addition Using Structure [with C program]

Learn: How to add two polynomials using structures in C? This article explains how to implement structure of polynomial, algorithm and C program for polynomial addition.
Submitted by Abhishek Jain, on June 20, 2017

What is Polynomial?

A polynomial is an expression that contains more than two terms. A term is made up of coefficient and exponent.
Example: P(x) = 4x3+6x2+7x+9

A polynomial may be represented using array or structure. A structure may be defined such that it contains two parts – one is the coefficient and second is the corresponding exponent. The structure definition may be given as shown below:

Struct polynomial{ 
	int coefficient; 
	int exponent;
};

The basic idea of polynomial addition is to add coefficient parts of the polynomials having same exponent.

Algorithm:

AddPoly(Struct Poly p1[10],Struct Poly p2[10],int t1,int t2,Struct Poly p3[10])
1.) [Initialize segment variables]
    [Initialize Counter] Set i=0,j=0,k=0

2.) Repeat step 3 while i<t1 and j<t2

3.) If p1[i].expo=p2[j].expo, then
p3[i].coeff=p1[i].coeff+p2[i].coeff
p3[k].expo=p1[i].expo
       [Increase counter] Set i=i+1,j=j+1,k=k+1
else if p1[i].expo > p2[j].expo, then 
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
       [Increase counter] Set i=i+1,k=k+1
else
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
       Set j=j+1,k=k+1
   [End of If]
  [End of loop]

4.) Repeat while i<t1 
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
       Set i=i+1,k=k+1
  [End of loop]

5.) Repeat while j<t2 
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
       Set j=j+1,k=k+1
  [End of loop]

6.) Return k
7.) Exit

C program for Polynomial Addition Using Structure

/* program for addition of two polynomials
 * polynomial are stored using structure
 * and program uses array of structure
 */
 #include<stdio.h>

 /* declare structure for polynomial */
 struct poly
 {
	 int coeff;
	 int expo;
 };
 /* declare three arrays p1, p2, p3 of type structure poly.
 * each polynomial can have maximum of ten terms
 * addition result of p1 and p2 is stored in p3 */

 struct poly p1[10],p2[10],p3[10];

 /* function prototypes */
 int readPoly(struct poly []);
 int addPoly(struct poly [],struct poly [],int ,int ,struct poly []);
 void displayPoly( struct poly [],int terms);

 int main()
 {
	int t1,t2,t3;

	/* read and display first polynomial */
	t1=readPoly(p1);
	printf(" \n First polynomial : ");
	displayPoly(p1,t1);
	/* read and display second polynomial */
	t2=readPoly(p2);
	printf(" \n Second polynomial : ");
	displayPoly(p2,t2);

	/* add two polynomials and display resultant polynomial */
	t3=addPoly(p1,p2,t1,t2,p3);
	printf(" \n\n Resultant polynomial after addition : ");
	displayPoly(p3,t3);
	printf("\n");

	return 0;
 }

 int readPoly(struct poly p[10])
 {
	int t1,i;

	printf("\n\n Enter the total number of terms in the polynomial:");
	scanf("%d",&t1);

	printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");
	for(i=0;i<t1;i++)
	{
		printf("   Enter the Coefficient(%d): ",i+1);
		scanf("%d",&p[i].coeff);
		printf("      Enter the exponent(%d): ",i+1);
		scanf("%d",&p[i].expo);        /* only statement in loop */
	}
	return(t1);
 }

 int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly p3[10])
 {
	int i,j,k;


	i=0;
	j=0;
	k=0;

	while(i<t1 && j<t2)
	{
		if(p1[i].expo==p2[j].expo)
		{
			p3[k].coeff=p1[i].coeff + p2[j].coeff;
			p3[k].expo=p1[i].expo;

			i++;
			j++;
			k++;
		}
		else if(p1[i].expo>p2[j].expo)
		{
			p3[k].coeff=p1[i].coeff;
			p3[k].expo=p1[i].expo;
			i++;
			k++;
		}
		else
		{
			p3[k].coeff=p2[j].coeff;
			p3[k].expo=p2[j].expo;
			j++;
			k++;
		}
	}

	/* for rest over terms of polynomial 1 */
	while(i<t1)
	{
		p3[k].coeff=p1[i].coeff;
		p3[k].expo=p1[i].expo;
		i++;
		k++;
	}
	/* for rest over terms of polynomial 2 */
	while(j<t2)
	{
		p3[k].coeff=p2[j].coeff;
		p3[k].expo=p2[j].expo;
		j++;
		k++;
	}

	return(k); /* k is number of terms in resultant polynomial*/
 }

 void displayPoly(struct poly p[10],int term)
 {
	int k;

	for(k=0;k<term-1;k++)
	printf("%d(x^%d)+",p[k].coeff,p[k].expo);
	printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);
}

Output

Enter the total number of terms in the polynomial:4
Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER
Enter the Coefficient(1): 3
Enter the exponent(1): 4
Enter the Coefficient(2): 7
Enter the exponent(2): 3
Enter the Coefficient(3): 5
Enter the exponent(3): 1
Enter the Coefficient(4): 8
Enter the exponent(4): 0

First polynomial : 3(x^4)+7(x^3)+5(x^1)+8(x^0)

Enter the total number of terms in the polynomial:5
Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER
Enter the Coefficient(1): 7
Enter the exponent(1): 5
Enter the Coefficient(2): 6
Enter the exponent(2): 4
Enter the Coefficient(3): 8
Enter the exponent(3): 2
Enter the Coefficient(4): 9
Enter the exponent(4): 1
Enter the Coefficient(5): 2
Enter the exponent(5): 0
Second polynomial : 7(x^5)+6(x^4)+8(x^2)+9(x^1)+2(x^0)

Resultant polynomial after addition : 7(x^5)+9(x^4)+7(x^3)+8(x^2)+14(x^1)+10(x^0)








COMMENTS