C Program for Polynomial Addition
What is Polynomial Addition?
Polynomial addition is adding two or more polynomials together to form a new polynomial. A polynomial is a mathematical expression consisting of variables and coefficients, where the coefficients are real numbers and the variables are raised to non-negative integer powers.
The general form of a polynomial is:
a_n x^n + a_{n-1} x^{n-1} + ... + a_1 x + a_0
Here, x is variable, the a_i are the coefficients, and n is the degree of the polynomial. We add the coefficients of each related term to add two polynomials together.
For example, to add:
2x^2 + 3x + 1
And
x^3 - 2x^2 + 4x - 3
We first arrange the terms in descending order of degree, like this:
x^3 + 2x^2 + 4x + (-3)
2x^2 + 3x + 1
Then, we add the coefficients of each related term:
x^3 + (2 + 0)x^2 + (4 + 3)x + (-3 + 1)
It simplifies to:
x^3 + 2x^2 + 7x - 2
So the sum of the two polynomials is x^3 + 2x^2 + 7x - 2.
Example:
First, we define a Polynomial struct that consists of a degree and an array of coefficients. We also define a maximum degree constant MAX_DEGREE to limit the size of the coefficient array.
typedef struct {
int degree;
float coef[MAX_DEGREE];
} Polynomial;
Next, we define a function add_polynomials that takes two Polynomial structs as input and returns the sum of the two polynomials in a third Polynomial struct. The function takes the first two polynomials as value parameters and the result polynomial as a pointer parameter, so that the function can modify the result polynomial.
void add_polynomials(Polynomial poly1, Polynomial poly2, Polynomial *result) {
int i;
// determine the degree of the result
if (poly1.degree > poly2.degree) {
result->degree = poly1.degree;
} else {
result->degree = poly2.degree;
}
// add the coefficients
for (i = 0; i <= result->degree; i++) {
if (i > poly1.degree) {
result->coef[i] = poly2.coef[i];
} else if (i > poly2.degree) {
result->coef[i] = poly1.coef[i];
} else {
result->coef[i] = poly1.coef[i] + poly2.coef[i];
}
}
}
In the add_polynomials function, we first determine the degree of the result polynomial by comparing the degrees of the input polynomials. We store the degree in the degree member of the result polynomial.
Then, we loop through each coefficient of the result polynomial and add the corresponding coefficients of the input polynomials. If the index i is greater than the degree of one of the input polynomials, we simply copy the coefficient from the other polynomial.
In the main function, we initialize two Polynomial structs with example values and call the add_polynomials function to compute their sum. We also print the original polynomials and the result to the console using a for loop.
int main() {
Polynomial poly1, poly2, result;
int i;
// initialize the first polynomial
poly1.degree = 2;
poly1.coef[0] = 1.0;
poly1.coef[1] = 2.0;
poly1.coef[2] = 3.0;
// initialize the second polynomial
poly2.degree = 3;
poly2.coef[0] = 4.0;
poly2.coef[1] = 5.0;
poly2.coef[2] = 6.0;
poly2.coef[3] = 7.0;
// add the polynomials
add_polynomials(poly1, poly2, &result);
// print the results
printf("Polynomial 1: ");
for (i = 0; i <= poly1.degree; i++) {
printf("%.1f ", poly1.coef[i]);
}
printf("\n");
printf("Polynomial 2: ");
for (i = 0; i <= poly2.degree; i++) {
printf("%.1f ", poly2.coef[i]);
}
printf("\n");
printf("Result: ");
for (i = 0; i <= result.degree; i++) {
printf("%.1f ", result.coef[i]);
}
printf("\n");
return 0
Output:
The output of the code would be:
Polynomial 1: 1.0 2.0 3.0
Polynomial 2: 4.0 5.0 6.0 7.0
Result: 4.0 7.0 9.0 7.0
The program first initializes two Polynomial structs with example values:
poly1.degree = 2;
poly1.coef[0] = 1.0;
poly1.coef[1] = 2.0;
poly1.coef[2] = 3.0;
poly2.degree = 3;
poly2.coef[0] = 4.0;
poly2.coef[1] = 5.0;
poly2.coef[2] = 6.0;
poly2.coef[3] = 7.0;
Then it calls the add_polynomials function to compute their sum:
add_polynomials(poly1, poly2, &result);
The add_polynomials function computes the sum of the two polynomials and stores the result in the result variable. The result polynomial has degree 3 and coefficients 4.0 7.0 9.0 7.0.
Finally, the program prints the original polynomials and the result to the console using a for loop:
Polynomial 1: 1.0 2.0 3.0
Polynomial 2: 4.0 5.0 6.0 7.0
Result: 4.0 7.0 9.0 7.0