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

Polynomial Code

DSA

Uploaded by

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

Polynomial Code

DSA

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
struct Term
{
int coefficient;
int exponent;
};
struct Polynomial
{
int numTerms;
struct Term *terms;
};
void createPolynomial(struct Polynomial *poly)
{
int i;
printf("Enter the number of terms: ");
scanf("%d", &poly->numTerms);
poly->terms = (struct Term *)malloc(poly->numTerms * sizeof(struct Term));

printf("Enter the terms:\n");


for (i = 0; i< poly->numTerms; i++)
scanf("%d%d", &poly->terms[i].coefficient, &poly->terms[i].exponent);
printf("\n");
}
void displayPolynomial(struct Polynomial poly)
{
int i;
for (i = 0; i<poly.numTerms; i++)
{
printf("%dx^%d", poly.terms[i].coefficient, poly.terms[i].exponent);
if (i + 1 <poly.numTerms)
printf(" + ");
}
printf("\n");
}
struct Polynomial *addPolynomials(struct Polynomial *poly1, struct Polynomial
*poly2)
{
int i, j, k;
struct Polynomial *sum;
sum = (struct Polynomial *)malloc(sizeof(struct Polynomial));
sum->terms = (struct Term *)malloc((poly1->numTerms + poly2->numTerms) *
sizeof(struct Term));
i = j = k = 0;
while (i< poly1->numTerms&& j < poly2->numTerms)
{
if (poly1->terms[i].exponent> poly2->terms[j].exponent)
sum->terms[k++] = poly1->terms[i++];
else if (poly1->terms[i].exponent< poly2->terms[j].exponent)
sum->terms[k++] = poly2->terms[j++];
else
{
sum->terms[k].exponent = poly1->terms[i].exponent;
sum->terms[k++].coefficient = poly1->terms[i++].coefficient + poly2-
>terms[j++].coefficient;
}
}
for (; i< poly1->numTerms; i++)
sum->terms[k++] = poly1->terms[i];
for (; j < poly2->numTerms; j++)
sum->terms[k++] = poly2->terms[j];
sum->numTerms = k;
return sum;
}
int main()
{
struct Polynomial poly1, poly2, *polySum;
printf("Enter Polynomial 1:\n");
createPolynomial(&poly1);
printf("Enter Polynomial 2:\n");
createPolynomial(&poly2);
polySum = addPolynomials(&poly1, &poly2);
printf("\n");
printf("Polynomial 1 is: ");
displayPolynomial(poly1);
printf("\n");
printf("Polynomial 2 is: ");
displayPolynomial(poly2);
printf("\n");
printf("Sum of the polynomials is: ");
displayPolynomial(*polySum);
free(poly1.terms);
free(poly2.terms);
free(polySum->terms);
free(polySum);
return 0;
}

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