Polynomial Addition Using an Array

Polynomial Addition Using an Array

Introduction to Polynomial

• Polynomials are algebraic expressions that consist of variables and coefficients. Variables are also sometimes called indeterminates.

• We can perform arithmetic operations such as addition, subtraction, multiplication, and positive integer exponentiation on polynomial expressions, but not division by a variable. An example of a polynomial with one variable is x2 + x - 12. In this example, there are three terms: x2, x, and -12.

 

Example of polynomials

• Linear Polynomial: P(x) = 2x + 3 )

• Quadratic Polynomial: Q(x) = x^2 - 4x + 4 )

• Cubic Polynomial: R(x) = x^3 + 3x^2 + 3x + 1 )

• Quartic Polynomial: S(x) = x^4 - 2x^3 + x^2 - x + 1 )

 

Representation of polynomial using array

• x^3 + 3x^2 + 3x + 1

• It is represented as




 • Like this we can represent the polynomial expression using the array

Adding polynomial using array

• To add two polynomials using arrays, you can represent each polynomial as an array where the index corresponds to the exponent of the term, and the value at that index corresponds to the term’s coefficient.

• Step-by-Step Approach:

• Initialize Arrays

• Input Coefficients

• Input Coefficients

• Output

Example problem

P(x)= 5x^4 - 9x^3 + 7x + 3

Q(x)= 2x^4 + 7x^2 + 8x + 3

In this problem, P and Q are the two expressions

Representing the array in array as below,


Cont. example problem

Case 1:

we’ve to compare the first two index of array on the both arrays.


Exp(p) == Exp(Q)

R=coeff(P)+coeff(Q)

5+2=7

Case 2:

we’ve to compare the next two index of array on the both arrays.

Exp(p) > Exp(Q)

Attach

Case 3:

Then we’ve to compare the next two index of array on the both arrays. Note: we’ve to increment the P and R . Q remains same.


Exp(p) < Exp(Q)

Attach

Case 4:

Then we’ve to compare the next two index of array on the both arrays.


Exp(p) == Exp(Q)

 R=coeff(P)+coeff(Q)

 7+8 = 7

 

Case 5:

Then we’ve to compare the next two index of array on the both arrays.


Exp(p) == Exp(Q)

R=coeff(P)+coeff(Q)

3+3 = 7

• Output:

The sum of two polynomials is as follows

R(x) = 7x^4 + 9x^3 + 7x^2 + 15x + 6

This the basic concept of adding two polynomials using an array.

Representation of Polynomials Using Arrays Code

#include <iostream>

#include <vector>

using namespace std;

// Class representing a Polynomial

class Polynomial {

private:

    vector<int> coeffs; // Vector to store coefficients, index represents the exponent

 

public:

    // Constructor initializes the polynomial with a given degree

    Polynomial(int degree) : coeffs(degree + 1, 0) {}

 

    // Function to insert or update a term in the polynomial

    void insertTerm(int coefficient, int exponent) {

        // Ensure the vector is large enough to hold this exponent

        if (exponent >= coeffs.size()) {

            coeffs.resize(exponent + 1, 0);

        }

        coeffs[exponent] = coefficient; // Set coefficient for the given exponent

    }

 

    // Function to add another polynomial to this one

    void addPolynomial(const Polynomial& other) {

        // Resize the current polynomial if it's smaller than the other

        if (other.coeffs.size() > coeffs.size()) {

            coeffs.resize(other.coeffs.size(), 0);

        }

        // Add coefficients of the other polynomial to this one

        for (size_t i = 0; i < other.coeffs.size(); ++i) {

            coeffs[i] += other.coeffs[i];

        }

    }

 

    // Function to display the polynomial

    void display() const {

        bool firstTerm = true;

        for (int i = coeffs.size() - 1; i >= 0; --i) {

            if (coeffs[i] != 0) {

                if (!firstTerm) {

                    cout << " + ";

                }

                cout << coeffs[i] << "x^" << i;

                firstTerm = false;

            }

        }

        if (firstTerm) {

            // If no term was printed, the polynomial is zero

            cout << "0";

        }

        cout << "\n";

    }

};

 

int main() {

    // Initialize a polynomial of degree 2

    Polynomial poly1(2);

    poly1.insertTerm(3, 2); // Insert term 3x^2

    poly1.insertTerm(4, 1); // Insert term 4x^1

 

    // Initialize another polynomial of degree 3

    Polynomial poly2(3);

    poly2.insertTerm(5, 3); // Insert term 5x^3

    poly2.insertTerm(1, 2); // Insert term 1x^2

 

    cout << "Polynomial 1: ";

    poly1.display(); // Display the first polynomial

 

    cout << "Polynomial 2: ";

    poly2.display(); // Display the second polynomial

 

    // Add poly2 to poly1

    poly1.addPolynomial(poly2);

 

    cout << "Result after addition: ";

    poly1.display(); // Display the result of the addition

 

    return 0;

}

Output:

Polynomial 1: 3x^2 +4x^1

Polynomial 2: 5x^3 +1x^2

Result after addition: 5x^3 +4x^2 +4x^1


Prepared by

Dineshwaran P N (23USC012)

Thiru Prashad G S (23USC041)

Sri Ramakrishna Mission Vidyalaya 

College of Arts and Science

Department of Computer Science Unaided Wing

 Coimbatore



Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Replies
    1. This comment has been removed by the author.

      Delete

Post a Comment

Popular posts from this blog

Radio Waves

DISPLAY CONTROLLER

Microwave