Monday, 9 September 2024

Write a Program to implement addition of two polynomials using 2 dimensional array.

 #include <iostream>

using namespace std;


#include<conio.h>


// Function to add two polynomials



void addPolynomials(int poly1[][2], int poly2[][2], int result[][2], int n)

 {

    for (int i = 0; i < n; i++) 

    {

  result[i][0] = poly1[i][0] + poly2[i][0]; 

    }

}


// Function to display a polynomial

void displayPolynomial(int poly[][2], int n)

 {

    for (int i = 0; i < n; i++)

   {

        cout << poly[i][0];

        if (i != 0)

            cout << "x^" << i; // Display power of x

        if (i != n - 1)

   cout << " + "; // Display '+' between terms in result

        

    }

cout << endl;

}


int main() 

{

    int n; // Degree of the polynomial + 1

     cout << "Enter the number of terms in the polynomials: ";

     cin >> n;


    int poly1[100][2], poly2[100][2], result[100][2]; 

// Arrays for the two polynomials and the result


    // Input for the first polynomial

  cout << "Enter the coefficients of the first polynomial:\n";

    for (int i = 0; i < n; i++) 

      {

cout << "Coefficient of x^" << i << ": ";

    cin >> poly1[i][0];

    }


    // Input for the second polynomial

  cout << "Enter the coefficients of the second polynomial:\n";

    for ( int i = 0; i < n; i++) 

    {

       cout << "Coefficient of x^" << i << ": ";

        cin >> poly2[i][0];

    }


    // Add the two polynomials

    addPolynomials(poly1, poly2, result, n);


    // Display the result

     cout << "First Polynomial: ";

   displayPolynomial(poly1, n);


   cout << "Second Polynomial: ";

    displayPolynomial(poly2, n);


    cout << "Resultant Polynomial after addition: ";

    displayPolynomial(result, n);

     getch();

    return 0;

}

Another Method

#include <iostream>


// Function to add two polynomials

             void addPolynomials(int poly1[][2], int poly2[][2], int result[][2], int  n) 

{

    for (int i = 0; i < n; i++) 

    {

        result[i][0] = poly1[i][0] + poly2[i][0]; // Add coefficients

        result[i][1] = poly1[i][1];               // Exponent remains the same

    }

}


// Function to display a polynomial

void displayPolynomial(int poly[][2], int n) 

{

    for (int i = 0; i < n; i++) 

    {

// Display coefficient and exponent


     cout << poly[i][0] << "x^" << poly[i][1];

         if (i != n - 1) 

        {

            cout << " + "; // Display '+' between terms

        }

    }

  cout << endl;

}


int main() 

{

    int n; // Number of terms in the polynomial

   cout << "Enter the number of terms in the polynomials: ";

    cin >> n;


    int poly1[100][2], poly2[100][2], result[100][2]; // Arrays for the two polynomials and the result


    // Input for the first polynomial

 cout << "Enter the coefficients and exponents of the first polynomial:\n";

    for (int i = 0; i < n; i++) 

    {

        cout << "Coefficient of term " << i + 1 << ": ";

        cin >> poly1[i][0]; // Coefficient

        cout << "Exponent of term " << i + 1 << ": ";

        cin >> poly1[i][1]; // Exponent

    }


    // Input for the second polynomial

  cout << "Enter the coefficients and exponents of the second polynomial:\n";

    for (int i = 0; i < n; i++) 

    {

      cout << "Coefficient of term " << i + 1 << ": ";

      cin >> poly2[i][0]; // Coefficient

      cout << "Exponent of term " << i + 1 << ": ";

       cin >> poly2[i][1]; // Exponent

    }


    // Add the two polynomials

    addPolynomials(poly1, poly2, result, n);


    // Display the result

    cout << "\nFirst Polynomial: ";

    displayPolynomial(poly1, n);


  cout << "Second Polynomial: ";

    displayPolynomial(poly2, n);


    cout << "Resultant Polynomial after addition: ";

    displayPolynomial(result, n);


    return 0;

}