We can represent a polynomial as a collection of terms and every term is having a coefficient and exponent. So, we can represent this as a list of terms having coefficients and exponents.
to represent polynomials.
#include <stdio.h>
struct Term {
int coeff;
int expo;
};
void addPolynomials(struct Term poly1[], int n1, struct Term poly2[], int n2, struct Term polyResult[], int *nResult) {
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
if (poly1[i].expo == poly2[j].expo) {
polyResult[k].coeff = poly1[i].coeff + poly2[j].coeff;
polyResult[k].expo = poly1[i].expo;
i++;
j++;
k++;
}
else if (poly1[i].expo > poly2[j].expo) {
polyResult[k] = poly1[i];
i++;
k++;
}
else {
polyResult[k] = poly2[j];
j++;
k++;
}
}
// Copy remaining terms of poly1
while (i < n1) {
polyResult[k] = poly1[i];
i++;
k++;
}
// Copy remaining terms of poly2
while (j < n2) {
polyResult[k] = poly2[j];
j++;
k++;
}
*nResult = k; // Total number of terms in result
}
void displayPolynomial(struct Term poly[], int n) {
for (int i = 0; i < n; i++) {
printf("%dx^%d", poly[i].coeff, poly[i].expo);
if (i != n - 1) {
printf(" + ");
}
}
printf("\n");
}
int main() {
struct Term poly1[] = { {5, 3}, {4, 2}, {2, 0} };
struct Term poly2[] = { {5, 2}, {5, 1}, {5, 0} };
struct Term polyResult[20];
int nResult;
int n1 = 3; // Number of terms in poly1
int n2 = 3; // Number of terms in poly2
printf("First Polynomial: ");
displayPolynomial(poly1, n1);
printf("Second Polynomial: ");
displayPolynomial(poly2, n2);
addPolynomials(poly1, n1, poly2, n2, polyResult, &nResult);
printf("Sum of Polynomials: ");
displayPolynomial(polyResult, nResult);
return 0;
}
Output
First Polynomial: 5x^3 + 4x^2 + 2x^0
Second Polynomial: 5x^2 + 5x^1 + 5x^0
Sum of Polynomials: 5x^3 + 9x^2 + 5x^1 + 7x^0
Note: Modify the code by adding a new function to read the polynomial.
Polynomial Addition with modified code - avoiding the terms with coefficient 0
#include <stdio.h>
struct Term {
int coeff;
int expo;
};
void addPolynomials(struct Term poly1[], int n1, struct Term poly2[], int n2, struct Term polyResult[], int *nResult) {
int i = 0, j = 0, k = 0,ncoeff;
while (i < n1 && j < n2) {
if (poly1[i].expo == poly2[j].expo) {
ncoeff=poly1[i].coeff + poly2[j].coeff;
if(ncoeff!=0)
{
polyResult[k].coeff = ncoeff;
polyResult[k].expo = poly1[i].expo;
k++;
}
i++;
j++;
}
else if (poly1[i].expo > poly2[j].expo) {
polyResult[k] = poly1[i];
i++;
k++;
}
else {
polyResult[k] = poly2[j];
j++;
k++;
}
}
// Copy remaining terms of poly1
while (i < n1) {
polyResult[k] = poly1[i];
i++;
k++;
}
// Copy remaining terms of poly2
while (j < n2) {
polyResult[k] = poly2[j];
j++;
k++;
}
*nResult = k; // Total number of terms in result
}
void displayPolynomial(struct Term poly[], int n) {
for (int i = 0; i < n; i++) {
printf("%dx^%d", poly[i].coeff, poly[i].expo);
if (i != n - 1) {
printf(" + ");
}
}
printf("\n");
}
int main() {
struct Term poly1[] = { {5, 3}, {4, 2}, {2, 0} };
struct Term poly2[] = { {-4, 2}, {5, 1}, {5, 0} };
struct Term polyResult[20];
int nResult;
int n1 = 3; // Number of terms in poly1
int n2 = 3; // Number of terms in poly2
printf("First Polynomial: ");
displayPolynomial(poly1, n1);
printf("Second Polynomial: ");
displayPolynomial(poly2, n2);
addPolynomials(poly1, n1, poly2, n2, polyResult, &nResult);
printf("Sum of Polynomials: ");
displayPolynomial(polyResult, nResult);
return 0;
}
First Polynomial: 5x^3 + 4x^2 + 2x^0
Second Polynomial: -4x^2 + 5x^1 + 5x^0
Sum of Polynomials: 5x^3 + 5x^1 + 7x^0
Polynomial Addition with modified code - reading the polynomials
#include <stdio.h>
struct Term {
int coeff;
int expo;
};
void addPolynomials(struct Term poly1[], int n1, struct Term poly2[], int n2, struct Term polyResult[], int *nResult) {
int i = 0, j = 0, k = 0, ncoeff;
while (i < n1 && j < n2) {
if (poly1[i].expo == poly2[j].expo) {
ncoeff = poly1[i].coeff + poly2[j].coeff;
if (ncoeff != 0) {
polyResult[k].coeff = ncoeff;
polyResult[k].expo = poly1[i].expo;
k++;
}
i++;
j++;
} else if (poly1[i].expo > poly2[j].expo) {
polyResult[k] = poly1[i];
i++;
k++;
} else {
polyResult[k] = poly2[j];
j++;
k++;
}
}
// Copy remaining terms of poly1
while (i < n1) {
polyResult[k] = poly1[i];
i++;
k++;
}
// Copy remaining terms of poly2
while (j < n2) {
polyResult[k] = poly2[j];
j++;
k++;
}
*nResult = k;
}
void displayPolynomial(struct Term poly[], int n) {
for (int i = 0; i < n; i++) {
printf("%dx^%d", poly[i].coeff, poly[i].expo);
if (i != n - 1) {
printf(" + ");
}
}
printf("\n");
}
int main() {
struct Term poly1[20], poly2[20], polyResult[40];
int n1, n2, nResult;
// Input first polynomial
printf("Enter the number of terms in the first polynomial: ");
scanf("%d", &n1);
printf("Enter the terms in descending order of exponents (coeff exponent):\n");
for (int i = 0; i < n1; i++) {
printf("Term %d: ", i + 1);
scanf("%d%d", &poly1[i].coeff, &poly1[i].expo);
}
// Input second polynomial
printf("Enter the number of terms in the second polynomial: ");
scanf("%d", &n2);
printf("Enter the terms in descending order of exponents (coeff exponent):\n");
for (int i = 0; i < n2; i++) {
printf("Term %d: ", i + 1);
scanf("%d%d", &poly2[i].coeff, &poly2[i].expo);
}
// Display input polynomials
printf("\nFirst Polynomial: ");
displayPolynomial(poly1, n1);
printf("Second Polynomial: ");
displayPolynomial(poly2, n2);
// Add polynomials
addPolynomials(poly1, n1, poly2, n2, polyResult, &nResult);
// Display result
printf("Sum of Polynomials: ");
displayPolynomial(polyResult, nResult);
return 0;
}
Enter the number of terms in the first polynomial: 3
Enter the terms in descending order of exponents (coeff exponent):
Term 1: 3 2
Term 2: 2 1
Term 3: 1 0
Enter the number of terms in the second polynomial: 2
Enter the terms in descending order of exponents (coeff exponent):
Term 1: 2 2
Term 2: 2 0
First Polynomial: 3x^2 + 2x^1 + 1x^0
Second Polynomial: 2x^2 + 2x^0
Sum of Polynomials: 5x^2 + 2x^1 + 3x^0
Comments
Post a Comment