C program function. Write a function called timesPolynomial( int coefficients1[]
ID: 3602961 • Letter: C
Question
C program function. Write a function called timesPolynomial( int coefficients1[], int degree1, int coefficients2[], int degree2, int coefficients3[], int degree3);
If you can please show pictures of it working. Thank you for your time.
timesPolnomial( int coefficients10. int degree1, int coefficients2[1. int degree2, int coefficients31. int degree3); Given two polynomials, represented by arrays coefficients1and coefficients2-with degree18 degree2 respectively. Compute the product polynomial and store the coefficients in coefficients3 with degree3. If the degree specified by degree3 is not large enough for the product, return 1 and set the coefficients in coefficients3 to zero to indicate an error. Otherwise, return 0 to indicate success. You can assume that degree1 and degree2 are accurate degree counts of coefficients1 and coefficients2 respectively NOTE: The degree of the product (degree3) is passed to you; it is not a return value. It indicates the number of coefficients that are provided in coefficients3. The product of the polynomial multiplication may be any degree less than or equal to degree3. Example: MULTIPLY Enter first polynomial f (x) x12 +6) Enter second polynomial g(x) (3x 3+5x +3) f(x) * g(x) = 3x^ 5 + 23x^3-3x^2-30x + 18Explanation / Answer
Here is the program , Enjoy!! :)
// C program to multiply two polynomials
#include <stdio.h>
// coefficients1[] represents coefficients of first polynomial
// coefficients2[] represents coefficients of second polynomial
// coefficients3[] will contain the product of polynomial1 and polynomial2
// degree1 and degree2 are degrees of coefficients1[] and coefficients2[] polynomial respectively
// degree3 is the degree of resultant polynomial
int timesPolynomial(int polynomial1[], int degree1, int polynomial2[], int degree2, int polynomial3[], int degree3)
{
if(degree3<(degree1+degree2-1)){ // degree provided for product polynomial is not large enough
for (int i = 0; i<degree3; i++)
polynomial3[i] = 0;
return 1;
}
else{ // degree provided for the product polynomial is large enough, let's compute the product
// Initialize the porduct polynomial
for (int i = 0; i<degree3; i++)
polynomial3[i] = 0;
// Multiply two polynomials term by term
// Take ever term of first polynomial
for (int i=0; i<degree1; i++)
{
// Multiply the current term of first polynomial
// with every term of second polynomial.
for (int j=0; j<degree2; j++)
polynomial3[i+j] += polynomial1[i]*polynomial2[j];
}
}
return 0;
}
// A utility function to print a polynomial
void printPoly(int polynomial[], int degree)
{
for (int i=0; i<degree; i++)
{
printf("%d",polynomial[i]);
if (i != 0)
printf("x^%d",i);
if (i != degree-1)
printf(" + ");
}
}
// 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 degree1,degree2,i;
int degree3 = degree1+degree2-1;
// create product polynomial
int *prod = new int[degree3];
printf("Enter degree of first polynomial m:");
scanf("%d",°ree1);
printf("Enter coefficients of first polynomial f(x):");
for(i=0;i<degree1;i++)
scanf("%d ",&A[i]);
printf("First polynomial is f(x):");
printPoly(A, degree1);
printf("Enter degree of second polynomial n:");
scanf("%d",°ree2);
printf("Enter coefficients of second polynomial g(x):");
for(i=0;i<degree2;i++)
scanf("%d ",&B[i]);
printf(" Second polynomial is g(x):");
printPoly(B, degree2);
printf("Enter degree of product polynomial r:");
scanf("%d",°ree3);
int flag = timesPolynomial(A, degree1, B, degree2, prod, degree3);
if(flag==0){
printf("Success!");
printf(" Product polynomial f(x)*g(x):");
printPoly(prod, degree3);
}
else{
printf("Error, degree entered for the product polynomial is not large enough!");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.