Your question only require the function padd but I am including other code for testing
#include<stdio.h>
#define MAX_DEGREE 101
typedef struct {
int degree;
float coef[MAX_DEGREE];
}polynomial;
/// ONLY REQUIRE BY YOUR QUESTION
void padd(polynomial* A,polynomial* B,polynomial *C){
C->degree=A->degree>B->degree?A->degree:B->degree;
//printf("%d",C->degree);
int i=0,j=0,x=0;
while(i<=A->degree &&
j<=B->degree){
if(A->degree-i==B->degree-j){
C->coef[x++]=A->coef[i++]+B->coef[j++];
}
else
C->coef[x++]=A->degree-i>B->degree-j?A->coef[i++]:B->coef[j++];
}
while(i<=A->degree)C->coef[x++]=A->coef[i++];
while(j<=B->degree)C->coef[x++]=B->coef[j++];
}
//END OF ANSWER
int main(){
polynomial A,B,C;
int i;
//let's take A=x^3+x^2+1
A.degree=3;
float a[MAX_DEGREE]={1,1,0,1};
for(i=0;i<MAX_DEGREE;i++)A.coef[i]=a[i];
// lets take B=X^2+X^1
B.degree=2;
float b[MAX_DEGREE]={1,1};
for(i=0;i<MAX_DEGREE;i++)B.coef[i]=b[i];
padd(&A,&B,&C);
for(i=0;i<A.degree;i++)printf("%f
",A.coef[i]);
printf("\n");
for(i=0;i<B.degree;i++)printf("%f
",B.coef[i]);
printf("\ndegree of C %d \n",C.degree);
for(i=0;i<=C.degree;i++)printf("%f
",C.coef[i]);
printf("\n");
return 0;
}
The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to...
please answer this question in python 3 For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial...
Please answer this in python 3, thank you. For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a...
Using C++ language, write a code to implement the "evaluate function" described below using the given poly.cpp file and poly.h header file. (ignore the other 3 functions if asked) poly.cpp file: poly.h header file: You are asked to implement four functions for a data structure used to store polynomials. The data structure used for polynomials is a linked list that consists of terms where each term consists of a coefficient, an exponent and a pointer to the next term. A...
Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...
A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are real numbers, and n is a non-negative integer. n is called the degree of polynomial. Every term in a polynomial consists of a coefficient and an exponent. For example, for the first term axn, a is the coefficient and n is the exponent. This assignment is about representing and computing...
The code should be written with python. Question 1: Computing Polynomials [35 marks A polynomial is a mathematical expression that can be built using constants and variables by means of addition, multiplication and exponentiation to a non-negative integer power. While there can be complex polynomials with multiple variable, in this exercise we limit out scope to polynomials with a single variable. The variable of a polynomial can be substituted by any values and the mapping that is associated with the...
Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...
/*********************************** * * Filename: poly.c * * ************************************/ #include "poly.h" /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE */ } /* end init_polynom */ /* Get inputs from user using scanf() and store them in the polynomial. */ void get_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE */ } /* end get_polynom */ /* Convert...
The extended specification of class Polynomial is shown below. You only need to implement the last four methods. The other features (i.e., constructor and operators) are given as part of the solution for tutorial 3. In the .cpp file for the new methods you need to include cmath that contains the definition of pow - raise to power.The goal of this problem set is to extend the solution of tutorial 3. In particular, we wish toadd methods to calculate a...