Answer to Question #251045 in C++ for SCADA HDMI

Question #251045

Use the array ADT to compute the addition and subtraction of two polynomials.

The interface of your program must look like this one:

Enter the degree of polynomial for p(x) : 5

Enter the coefficient of x^0 : 0

Enter the coefficient of x^1 : 4

Enter the coefficient of x^2 : 5

Enter the coefficient of x^3 : 0

Enter the coefficient of x^4 : 6

Enter the coefficient of x^5 : 4

Enter the degree of polynomial for q(x) : 3

Enter the coefficient of x^0 : 3

Enter the coefficient of x^1 : -2

Enter the coefficient of x^2 : 3

Enter the coefficient of x^3 : 10

First Polynomial p(x) = 4x^5 + 6x^4 + 5x^2 + 4x

Second Polynomial q(x) = 10x^3 + 3x^2 – 2x + 3

p(x) + q(x) = 4x^5 + 6x^4 +10x^3 + 8x^2 + 2x + 3

p(x) – q(x) = 4x^5 + 6x^4 – 10x^3 + 2x^2 + 6x – 3


1
Expert's answer
2021-10-14T04:50:02-0400
#include <iostream>
using namespace std;
int max(int m, int n) { return (m > n)? m: n; }


int *add(int A[], int B[], int m, int n)
{
int size = max(m, n);
int *sum = new int[size];


for (int i = 0; i<m; i++)
	sum[i] = A[i];


for (int i=0; i<n; i++)
	sum[i] += B[i];


return sum;
}


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 << " + ";
	}
}


int main()
{
	int A[] = {5, 0, 10, 6};


	int B[] = {1, 2, 4};
	int m = sizeof(A)/sizeof(A[0]);
	int n = sizeof(B)/sizeof(B[0]);


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


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


	cout << "\nsum is \n";
	printPoly(sum, size);


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS