0% found this document useful (0 votes)
2 views

Structure

The document contains a C program that adds two polynomials represented as arrays of terms, each with a coefficient and exponent. It defines a structure for polynomial terms, functions to add polynomials and display the result, and includes a main function for user input and output. The example output demonstrates the addition of two polynomials resulting in a new polynomial.

Uploaded by

ishapandit1603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Structure

The document contains a C program that adds two polynomials represented as arrays of terms, each with a coefficient and exponent. It defines a structure for polynomial terms, functions to add polynomials and display the result, and includes a main function for user input and output. The example output demonstrates the addition of two polynomials resulting in a new polynomial.

Uploaded by

ishapandit1603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Structure -Addition of Polynomials

#include <stdio.h>

struct Term {
int coeff;
int exp;
};

void addPolynomials(struct Term p1[], int n1, struct Term p2[], int n2, struct
Term result[], int *nResult) {
int i = 0, j = 0, k = 0;

while (i < n1 && j < n2) {


if (p1[i].exp == p2[j].exp) {
result[k++] = (struct Term){p1[i].coeff + p2[j].coeff, p1[i].exp};
i++; j++;
} else if (p1[i].exp > p2[j].exp) {
result[k++] = p1[i++];
} else {
result[k++] = p2[j++];
}
}
while (i < n1) result[k++] = p1[i++];
while (j < n2) result[k++] = p2[j++];
*nResult = k;
}

void displayPolynomial(struct Term poly[], int n) {


for (int i = 0; i < n; i++)
printf("%dx^%d%s", poly[i].coeff, poly[i].exp, (i < n - 1) ? " + " : "\n");
}

int main() {
int n1, n2, nResult;
struct Term p1[10], p2[10], result[20];

printf("Enter number of terms in 1st polynomial: "); scanf("%d", &n1);


printf("Enter terms (coeff exp):\n");
for (int i = 0; i < n1; i++) scanf("%d %d", &p1[i].coeff, &p1[i].exp);

printf("Enter number of terms in 2nd polynomial: "); scanf("%d", &n2);


printf("Enter terms (coeff exp):\n");
for (int i = 0; i < n2; i++) scanf("%d %d", &p2[i].coeff, &p2[i].exp);

addPolynomials(p1, n1, p2, n2, result, &nResult);


printf("Result: ");
displayPolynomial(result, nResult);

return 0;
}
Output
Enter number of terms in 1st polynomial: 2
Enter terms (coeff exp):
42
61
Enter number of terms in 2nd polynomial: 2
Enter terms (coeff exp):
52
31
Result: 9x^2 + 9x^1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy