Professional Documents
Culture Documents
pract3DS W 23
pract3DS W 23
pract3DS W 23
AIM:-
Write and implement an algorithm which reads two polynomials p(x,y,z) and g(x,y,z) from the
user store it in memory ,perform the addition of two polynomials and display resultant
polynomial in Descending order h(x,y,z)= p(x,y,z) +g(x,y,z)
Algorithm:
Assume that there are two polynomials A and B. The polynomial storing the addition result
is stored in one large array C and i,j,k represent the pointers to the polynomials and arrays resp.
1. Read n1=no of terms in A, n2=no. of terms in B.
2. while i<n1 and j<n2
a) If exp at ith position of A=exp at jth position of B, then
C at kth position =coeff of A at ith position +coeff of B at jth position.
Increment i,j,k to point to next position in A,B,C.
b) Else
If exp position i>exp position j in B then
Copy coeff at ith position from A to coeff at kth position in C.
Copy exp pointed by i into exponent field at position k in C
Increment i,k
Else
Copy coeff at jth position from B to coeff at kth position in C
Copy exp pointed by j into exponent field at position k in C
Increment j,k.
3. While i<n1
a) Copy coeff at ith position from A to coeff at kth position in C.
b) Copy exp pointed by i into exponent field at position k in C
c) Increment i,k
4. While j<n2
a) Copy coeff at jth position from B to coeff at kth position in C
b) Copy exp pointed by j into exponent field at position k in C
c) Increment j,k.
5. Display C[result of array addition]
Department of Information Technology, GCoE, Amravati Winter 2023
6. Exit.
Sample Output:-
Enter the number of terms in first polynomial
5
Enter its coefficients of every term
8 -6 3 2 1
Enter the Power of x for each term:-
2 0 3 1 0
Enter the Power of y for each term:-
2 1 1 7 0
Enter the Power of z for each term:-
1 8 1 1 0
The entered first polynomial is
p(x,y,z) = 8x2y2z – 6 yz8 + 3x3yz + 2 xy7z + 1
Enter the number of terms in Second polynomial
3
Enter its coefficients of every term
12 5 7
Enter the Power of x for each term:-
Enter the Power of y for each term:-
Enter the Power of z for each term:-
The entered second polynomial is
g(x,y,z) = 12x2y3z + 5x4y2z + 7
The resultant polynomial contains
h(x,y,z) = 5x4y2z + 3x3yz + 12x2y3z + 8x2y2z + 2 xy7z - 6 yz8 + 8
struct node
{
int co;
int p1;
int p2;
int p3;
struct node *next;
};
return head;
}
while (y != NULL)
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->co = y->co;
newNode->p1 = y->p1;
newNode->p2= y->p2;
newNode->p3 = y->p3;
newNode->next = NULL;
y = y->next;
r->next = newNode;
r = newNode;
}
return head;
}
int main()
{
printf("Enter the first polynomial:\n");
a = createPoly();
return 0;
}