Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program should allow the user to input the two polynomials and the monomial

ID: 3885778 • Letter: T

Question

The program should allow the user to input the two polynomials and the monomial multiplier. Then output the following: each of the two original polynomials, the sum of the polynomials, the difference (first polynomial minus the second), another difference (second polynomial minus the first), the result for each of the two polynomials being multiplied by a (different) monomial. Test data included is:

Polynomial #1: 2x^2-1 Polynomial #2: x^2+2x+3 Monomial: 3x^4 -------- Test Data: Polynomial #1 0x+1 ---------- Test Data: Polynomial #1 3x^-2-4

Task: Use linked lists to represent two polynomials. Each polynomial will be an object of the Polynomial class. Each node in the linked list will represent a term of the polynomial, so data members of the nodes will include coefficient and exponent.

Processing: The program should accept appropriate values (i.e., coefficient and exponent values) from the user to populate two polynomials. Simple data checking should verify that the integer exponents are non-negative and that coefficients are not zero. Polynomials may have varying numbers of terms. The terms should be stored in the linked list in decreasing order of their exponents.

For example, valid polynomials might be:

3x^7-1.5x^4+3 (stored as 3 nodes)

7.1x^2-6x (stored as 2 nodes)

12 (stored as 1 node)

The program will perform three basic operations on the polynomials:

add the two original polynomials together; output the sum

subtract one of the original polynomials from the other one; output the difference

perform monomial multiplication on one of the original polynomials (i.e., multiply each polynomial term by a given monomial term, not necessarily a scalar); output the monomial and the result

Note: do not change the original two polynomials during the addition, subtraction or multiplication operations. Include appropriate functions in the class (constructors, etc.) to produce an effective object-oriented program.

Input: Be sure the program is flexible enough that polynomial terms may be supplied by the user out of proper order, but still be stored in proper order.

Output: Test program should allow the user to input the two polynomials and the monomial multiplier. Then output the following:

each of the two original polynomials

the sum of the polynomials

the difference (first polynomial minus the second)

another difference (second polynomial minus the first)

the result for each of the two polynomials being multiplied by a (different) monomial

Make sure your program in program is well documented both internally and externally.

Explanation / Answer

// Simple C++ program to add two polynomials
#include <iostream>
using namespace std;

// A utility function to return maximum of two integers
int max(int m, int n) { return (m > n)? m: n; }

// A[] represents coefficients of first polynomial
// B[] represents coefficients of second polynomial
// m and n are sizes of A[] and B[] respectively
int *add(int A[], int B[], int m, int n)
{
int size = max(m, n);
int *sum = new int[size];

// Initialize the porduct polynomial
for (int i = 0; i<m; i++)
sum[i] = A[i];

// Take ever term of first polynomial
for (int i=0; i<n; i++)
sum[i] += B[i];

return sum;
}

// A utility function to print a polynomial
void printPoly(int poly[], int n)
{
for (int i=0; i<n; i++)
{
cout << poly[i];
if (i != 0)
cout << "x^" << i ;
if (i != n-1)
cout << " + ";
}
}

// Driver program to test above functions
int main()
{
// The following array represents polynomial 5 + 10x^2 + 6x^3
int A[] = {5, 0, 10, 6};

// The following array represents polynomial 1 + 2x + 4x^2
int B[] = {1, 2, 4};
int m = sizeof(A)/sizeof(A[0]);
int n = sizeof(B)/sizeof(B[0]);

cout << "First polynomial is ";
printPoly(A, m);
cout << " Second polynomial is ";
printPoly(B, n);

int *sum = add(A, B, m, n);
int size = max(m, n);

cout << " sumuct polynomial is ";
printPoly(sum, size);

return 0;
}