Branch Unspecified
27 Nov 2008

# C function for addition of two single variable polynomials

can any1 tel me wat may be c code for writing function to add 2 single variable polynomials of length say n&m?

Branch Unspecified
11 years ago
c code

can anyone giv me c code for addition of two single variable polynomials.

mahul

Branch Unspecified
11 years ago
please be a little more elaborate if u can, like how u represent and store ur polynomials, the arguments u would like to pass to ur function and such stuff. By what u have posted, there was very little that i could comprehend.

Branch Unspecified
11 years ago
i want to accept any length two polynomials like say 6x^3+5x^2
and 4x^2+5x+7
now i hav to take powers and coefficients of x from user.i hav done that.
but now i need to add these polynomials.

friendster7

Branch Unspecified
11 years ago
mynode *polynomial_add(mynode *h1, mynode *h2, mynode *h3)
{
mynode *p1, *p2;
int x1, x2, y1, y2, cf1, cf2, cf;

p1 = h1->next;

while(p1!=h1)
{
x1 = p1->px;
y1 = p1->py;
cf1 = p1->cf;

// Search for this term in the second polynomial

p2 = h2->next;

while(p2 != h2)
{
x2 = p2->px;
y2 = p2->py;
cf2 = p2->cf;

if(x1 == x2 && y1 == y2)break;

p2 = p2->next;

}

if(p2 != h2)
{
// We found something in the second polynomial.

cf = cf1 + cf2;
p2->flag = 1;

}
else
{
}

p1 = p1->next;

}//while

// Add the remaining elements of the second polynomail to the result

while(p2 != h2)
{
if(p2 -> flag ==0)
{
}
p2=p2->next;
}

return(h3);
}
this will help u i guess..this can add two polynomials

Ashutosh_shukla

Branch Unspecified
10 years ago
I just joined the site and would like to contribute in here
Code in PASCAL using linked list
```PROGRAM polyaddll(input,output);
TYPE nodeptr=^node;
node=RECORD
coef:integer;
exp:integer;
END;
VAR one,two,res:nodeptr;
k:integer;
PROCEDURE print(n:nodeptr);
VAR p:nodeptr;
BEGIN
p:=n;
WHILE (p<>NIL) DO
BEGIN
write(p^.coef,' X^ ',p^.exp);
write(' + ');
END;
writeln;
END;
PROCEDURE create(VAR n:nodeptr);
VAR p,q:nodeptr;
c:char;
BEGIN
c:='y';
WHILE (c<>'n') DO
BEGIN
new(p);
write('Enter the coefficient of the term : ');
p^.coef:=k;
write('Enter the exponent of the term : ');
p^.exp:=k;
write('Do you want to add any more terms (Y/N) : ');
IF (n=NIL) THEN
BEGIN
n:=p;
q:=p;
END
ELSE
BEGIN
q:=p;
END;
END;
END;
PROCEDURE sum(VAR a,b,c:nodeptr);
VAR p,q,r,s:nodeptr;
BEGIN
p:=a;
q:=b;
WHILE ((p<>NIL) AND (q<>NIL)) DO
BEGIN
new(r);
IF (p^.exp>q^.exp) THEN
BEGIN
r^.coef:=p^.coef;
r^.exp:=p^.exp;
END
ELSE
IF (q^.exp>p^.exp) THEN
BEGIN
r^.coef:=q^.coef;
r^.exp:=q^.exp;
END
ELSE
BEGIN
r^.coef:=p^.coef+q^.coef;
r^.exp:=p^.exp;
END;
IF (c=NIL) THEN
BEGIN
c:=r;
s:=r;
END
ELSE
BEGIN
s:=r;
END;
END;
WHILE (p<>NIL) DO
BEGIN
new(r);
r^.coef:=p^.coef;
r^.exp:=p^.exp;
IF (c=NIL) THEN
BEGIN
c:=r;
s:=r;
END
ELSE
BEGIN
s:=r;
END;
END;
WHILE (q<>NIL) DO
BEGIN
new(r);
r^.coef:=q^.coef;
r^.exp:=q^.exp;
IF (c=NIL) THEN
BEGIN
c:=r;
s:=r;
END
ELSE
BEGIN
s:=r;
END;
END;
END;
BEGIN
one:=NIL;
two:=NIL;
res:=NIL;
writeln('Enter the first polynomial : ');
create(one);
writeln('Enter the second polynomial : ');
create(two);
writeln('The first polynomial is  : ');
print(one);
writeln('The second polynomial is : ');
print(two);
sum(one,two,res);
writeln('The reult of addition is : ');
print(res);
END.```
We Rock!!!!!!!

Branch Unspecified
10 years ago
Hi friendster7,

I did not tried running your code but here are some of my view:
1. Since you are initilizing p1 & p2 with next of h1 & h2 and then comparing for (p1 != h1) and (p2 != h2), the conditions will not meet.
2. Code for addNode is required.

Here is my sample code for the given problem statement.

```#include "stdio.h"

typedef struct node {
int cof;
int pow;
struct node *next;
} nodet;

nodet* createnewnode(int cof, int pow) {
nodet* newnode = (nodet*)malloc(sizeof(nodet));
newnode->cof = cof;
newnode->pow = pow;
newnode->next = NULL;
return newnode;
}

nodet* temp = NULL;
free(temp);
}
}

printf("Empty polynomial\n");
} else {
do {
printf("+");
}
printf("1");
} else {
}
printf("x");
}
printf(" ");
}
}
printf("\n");
}
}

nodet* addpoly(nodet* poly1, nodet* poly2) {
nodet* newnode = NULL;
nodet* retnode = NULL;
nodet* retnodelast = NULL;
nodet* temp = NULL;
nodet* temp1 = poly1;
nodet* temp2 = poly2;
int cof = 0;
int pow = 0;

while((temp1 != NULL) && (temp2 != NULL)) {
if(temp1->pow > temp2->pow) {
cof = temp1->cof;
pow = temp1->pow;
temp1 = temp1->next;
} else if(temp1->pow == temp2->pow) {
cof = temp1->cof + temp2->cof;
pow = temp1->pow;
temp1 = temp1->next;
temp2 = temp2->next;
} else {
cof = temp2->cof;
pow = temp2->pow;
temp2 = temp2->next;
}
if(cof != 0) {
newnode = createnewnode(cof, pow);
if(retnode == NULL) {
retnode = newnode;
retnodelast = retnode;
} else {
retnodelast->next = newnode;
retnodelast = retnodelast->next;
}
}
}

if(temp1 != NULL) {
temp = temp1;
} else {
temp = temp2;
}

while(temp != NULL) {
newnode = createnewnode(temp->cof, temp->pow);
if(retnode == NULL) {
retnode = newnode;
retnodelast = retnode;
} else {
retnodelast->next = newnode;
retnode = retnode->next;
}
temp = temp->next;
}

return retnode;
}

int main() {
nodet *poly1 = NULL;
nodet *poly2 = NULL;
nodet *polysum = NULL;
nodet* newnode = NULL;
nodet* temp = NULL;
int order_poly1 = 0;
int order_poly2 = 0;
int cof = 0;
int i = 0;

scanf("%d", &order_poly1);
if(order_poly1 < 0) {
printf("Polynomial of invalid order\n");
return 0;
}

for(i = order_poly1; i >= 0; i--) {
scanf("%d", &cof);
if(cof != 0) {
newnode = createnewnode(cof, i);
if(poly1 == NULL) {
poly1 = newnode;
temp = poly1;
} else {
temp->next = newnode;
temp = temp->next;
}
}
}

scanf("%d", &order_poly2);
if(order_poly2 < 0) {
printf("Polynomial of invalid order\n");
deletepoly(poly1);
return 0;
}

for(i = order_poly2; i >= 0; i--) {
scanf("%d", &cof);
if(cof != 0) {
newnode = createnewnode(cof, i);
if(poly2 == NULL) {
poly2 = newnode;
temp = poly2;
} else {
temp->next = newnode;
temp = temp->next;
}
}
}

//display polynomial 1
printf("Polynomial 1:\n");
displaypoly(poly1);

//display polynomial 2
printf("Polynomial 2:\n");
displaypoly(poly2);

//add polynomials & display resultant polynomial
printf("Result Polynomial:\n");
displaypoly(polysum);

//delete polynomials
deletepoly(poly1);
deletepoly(poly2);
deletepoly(polysum);

return 0;
}
```
Compile the file with above code and run as:
a.exe < input.txt > output.txt [on windows]
a.out < input.txt > output.txt [on linux]

Here,
a.exe or a.out are the executables generated after compilation

input.txt is the file containing input, e.g., if the polynomials are
P1: 4x^2 + x + 1
P2: 5x^3 -4x^2 - 1
Then input.txt will be
2
4
1
1
3
5
-4
0
-1
2
4
1
1

2 represent order of polynomial (4x^2 + x + 1) and then each number represents coefficients
3
5
-4
0
-1

3 represent order of polynomial (5x^3 -4x^2 - 1) and then each number represents coefficients
output.txt is the output file containing result, for given example output.txt will contain
Polynomial 1:
+4x^2 +x +1
Polynomial 2:
+5x^3 -4x^2 -1
Result Polynomial:
+5x^3 +x