pract3DS W 23

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Practical No-3

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

Department of Information Technology, GCoE, Amravati Winter 2023


Source Code:-
#include <stdio.h>
#include <stdlib.h>

struct node *a, *b, *r;

struct node
{
int co;
int p1;
int p2;
int p3;
struct node *next;
};

struct node *createPoly()


{
int i, n;
struct node *head = NULL;
struct node *p, *q;

printf("Enter the number of terms for the polynomial: ");


scanf("%d", &n);

for (i = 0; i < n; i++)


{

int co, p1,p2,p3;


printf("Enter the coefficient for term %d: ", i + 1);
scanf("%d", &co);
printf("Enter the x power for term %d: ", i + 1);
scanf("%d", &p1);
printf("Enter the y power for term %d: ", i + 1);
scanf("%d", &p2);
printf("Enter the z power for term %d: ", i + 1);
scanf("%d", &p3);

// Create a new node for the term


q = (struct node *)malloc(sizeof(struct node));
q->co = co;
q->p1= p1;
q->p2 = p2;
q->p3= p3;
q->next = NULL;

Department of Information Technology, GCoE, Amravati Winter 2023


if (head == NULL)
{
head = q;
p = head;
}
else
{
p->next = q;
p = p->next;
}
}

return head;
}

void displayPoly(struct node *head)


{
printf("Polynomial is: ");
struct node *p = head;
while (p != NULL)
{
printf("%dx^%dy^%dz^%d ", p->co, p->p1, p->p2, p->p3);
p = p->next;
if(p!=0) printf("+");
}
printf("\n");
}

struct node *addition()


{
struct node *newNode, *x, *y, *r, *head = NULL;
x = a;
y = b;
while (x != NULL && y != NULL)
{
if (x->p1 == y->p1 && x->p2 == y->p2 && x->p3 == y->p3)
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->co = x->co + y->co;
newNode->p1= x->p1;
newNode->p2 = x->p2;
newNode->p3 = x->p3;
newNode->next = NULL;
x = x->next;
Department of Information Technology, GCoE, Amravati Winter 2023
y = y->next;
}
else if (x->p1> y->p1)
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->co = x->co;
newNode->p1 = x->p1;
newNode->p2 = x->p2;
newNode->p3 = x->p3;
newNode->next = NULL;
x = x->next;
}
else
{
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;
}
if (head == NULL)
{
head = newNode;
r = head;
}
else
{
r->next = newNode;
r = newNode;
}
}

// Append the remaining terms of x and y, if any


while (x != NULL)
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->co = x->co;
newNode->p1 = x->p1;
newNode->p2= x->p2;
newNode->p3 = x->p3;
newNode->next = NULL;
x = x->next;
r->next = newNode;
Department of Information Technology, GCoE, Amravati Winter 2023
r = newNode;
}

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();

printf("Enter the second polynomial:\n");


b= createPoly();

printf("First polynomial is:\n");


displayPoly(a);

printf("Second polynomial is:\n");


displayPoly(b);

printf("The sum of your polynomial is:\n");


r = addition();
displayPoly(r);

return 0;
}

Department of Information Technology, GCoE, Amravati Winter 2023


Output:-
Enter the first polynomial:
Enter the number of terms for the polynomial: 2
Enter the coefficient for term 1: 1
Enter the x power for term 1: 1
Enter the y power for term 1: 2
Enter the z power for term 1: 3
Enter the coefficient for term 2: 2
Enter the x power for term 2: 3
Enter the y power for term 2: 4
Enter the z power for term 2: 5
Enter the second polynomial:
Enter the number of terms for the polynomial: 2
Enter the coefficient for term 1: 1
Enter the x power for term 1: 1
Enter the y power for term 1: 2
Enter the z power for term 1: 3
Enter the coefficient for term 2: 2
Enter the x power for term 2: 3
Enter the y power for term 2: 4
Enter the z power for term 2: 5
First polynomial is:
Polynomial is: 1x^1y^2z^3 +2x^3y^4z^5
Second polynomial is:
Polynomial is: 1x^1y^2z^3 +2x^3y^4z^5
The sum of your polynomial is:
Polynomial is: 2x^1y^2z^3 +4x^3y^4z^5

Department of Information Technology, GCoE, Amravati Winter 2023


Viva Question:-
 How to store polynomials in memory
 How to check degree of term in polynomial
 What is the advantage of using a linked list for representing polynomials over an array?
 How to sort polynomial using arrays?

Department of Information Technology, GCoE, Amravati Winter 2023

You might also like