Multiplication of polynomials in C++

In this tutorial, we will learn how to perform multiplication of polynomials in C++.

Before proceeding to the solution, let us understand what a polynomial is:

  • A polynomial is an expression.
  • It consists of variables that are also called indeterminates and coefficients.
  • The operations are addition, subtraction, multiplication, and non-negative integer exponents of variables.

Example of a polynomial:

5x^3 + 4x^2 + 7x^1 + 12

This polynomial has four terms. The first term has coefficient 5 and the degree of the variable x is 3. The next two terms have coefficients 4 and 7 with degrees 2 and 1 respectively. The last term has a coefficient of 12 and the degree is 0. So, we can omit writing the variable as any variable to the power of zero equates to 1.

Program to perform multiplication of polynomials in C++:

#include<iostream>
using namespace std;
int multiplypolynomial(int (&x)[100][2],int (&y)[100][2],int (&z)[1000][2],int num1,int num2)
{ 
    int r=0;
    for(int m=0;m<num2;m++)
    { 
        for(int n=0;n<num1;n++,r++)
        { 
            z[r][0]=x[n][0]*y[m][0];
            z[r][1]=x[n][1]+y[m][1];
        } 
    } 
    for(int m=0;m<r;m++) 
    { 
        for(int n=m+1;n<r;n++) 
        { 
            if(z[m][1]==z[n][1]) 
            { 
                z[m][0]+=z[n][0]; 
                for(int l=n;l<r-1;l++) 
                { 
                    z[l][0]=z[l+1][0]; 
                    z[l][1]=z[l+1][1]; 
                } 
                r--;
            } 
        } 
    } 
    return r; 
} 
void printpolynomial(int (&g)[100][2],int num) 
{ 
    for(int j=0;j<num-1;j++) 
    cout<<g[j][0]<<"*x^"<<g[j][1]<<"+"; 
    cout<<g[num-1][0]<<"*x^"<<g[num-1][1]<<endl; 
} 
void getpolynomial(int (&g)[100][2],int &num) 
{ 
    cout<<"How many terms are there in the polynomial ?"<<endl; 
    cin>>num; 
    cout<<"Enter the coefficient and the exponent of the polynomial terms in the decreasing order: "<<endl; 
    for(int j=0;j<num;j++) 
    cin>>g[j][0]>>g[j][1]; 
} 
int main() 
{ 
    int x[100][2],y[100][2],z[1000][2],num1,num2,num3; 
    getpolynomial(x,num1); 
    printpolynomial(x,num1); 
    getpolynomial(y,num2); 
    printpolynomial(y,num2); 
    num3=multiplypolynomial(x,y,z,num1,num2); 
    cout<<"The result of the multiplication of two polynomials: "<<endl; 
    for(int j=0;j<num3-1;j++) 
    cout<<z[j][0]<<"*x^"<<z[j][1]<<"+"; 
    cout<<z[num3-1][0]<<"*x^"<<z[num3-1][1]<<endl; 
    return 0; 
}

Output:

How many terms are there in the polynomial ?
3
Enter the coefficient and the exponent of the polynomial terms in the decreasing order: 
5
3
4
2
7
1
5*x^3+4*x^2+7*x^1
How many terms are there in the polynomial ?
2
Enter the coefficient and the exponent of the polynomial terms in the decreasing order: 
5
1
4
0
5*x^1+4*x^0
The result of the multiplication of two polynomials: 
25*x^4+40*x^3+51*x^2+28*x^1

The above program performs the multiplication of two polynomials. It accepts the count of the number of terms. It then accepts the coefficients and the degrees of the polynomial terms. Then it performs multiplication by considering each variable in both the polynomials.

For this use the two-dimensional arrays to store the polynomial terms along with its corresponding degree. Then after the multiplication operation, again store the result in a two-dimensional array. Finally, display the result on the terminal.

Thanks for reading!

Recommended Posts:
Topological Sort in C++

Leave a Reply

Your email address will not be published. Required fields are marked *