I wrote C program to which should add two polynomials. I wrote this program in Kali Linux 2.0 OS. When I execute the program I don't get the required output. Instead I get this-
Polynomial 1
How many no. terms do you want to enter? 1
Enter coefficient for term 1: 2
Enter exponent for term 1: 3
Polynomial 2
How many no. terms do you want to enter? 1
Enter coefficient for term 1: 2
Enter exponent for term 1: 3
Polynomial 1=
Polynomial 2=
Sum of the two polynomials is
The program code is given below-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int exp,coeff;
struct node *next;
}poly;
poly *headA,*headB,*headC;
poly *lastA,*lastB,*lastC;
void insert(poly*,poly*,poly *);
void input(poly *,poly *);
void display(poly *);
void insert(poly *new,poly *head,poly *last)
{
poly *p,*q;
if(head==NULL&&last==NULL) //setting the start
{
head=last=new;
return;
}
p=head;
q=NULL;
while(new->exp<p->exp)
{
q=p;
p=p->next;
}
if(p->exp==new->exp) //if exponents are equal
p->coeff=p->coeff+new->coeff;
else
{
if(q!=NULL) //insertion in middle
{
q->next=new;
new->next=p;
}
else if(q==NULL) //insertion at beginning
{
new->next=head;
head=new;
}
else if(p==NULL) //insertion at the end
{
last->next=new;
last=new;
}
}
}
void input(poly *head,poly *last)
{
int i,n,c,e;
poly *new;
new=(poly *)malloc(sizeof(poly));
printf("How many no. terms do you want to enter? ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter coefficient for term %d: ",i);
scanf("%d",&new->coeff);
printf("Enter exponent for term %d: ",i);
scanf("%d",&new->exp);
new->next=NULL;
insert(new,head,last);
insert(new,headC,lastC);
}
}
void display(poly *start)
{
poly *p;
p=start;
while(p!=NULL)
{
printf("(%dx^%d)+",p->coeff,p->exp);
p=p->next;
}
printf("\b");
}
void main()
{
system("clear");
headA=(poly *)malloc(sizeof(poly));
headB=(poly *)malloc(sizeof(poly));
headC=(poly *)malloc(sizeof(poly));
lastA=(poly *)malloc(sizeof(poly));
lastB=(poly *)malloc(sizeof(poly));
lastC=(poly *)malloc(sizeof(poly));
headA=headB=headC=NULL;
lastA=lastB=lastC=NULL;
printf("Polynomial 1\n\n");
input(headA,lastA);
printf("\nPolynomial 2\n\n");
input(headB,lastB);
printf("\n\nPolynomial 1=");
display(headA);
printf("\nPolynomial 2=");
display(headB);
printf("\nSum of the two polynomials is=");
display(headC);
}
Edit: After reading the comments I brought some changes in my code. Now the output is-
Polynomial 1
How many no. terms do you want to enter? 1
Enter coefficient for term 1: 5
Enter exponent for term 1: 6
Polynomial 2
How many no. terms do you want to enter? 2
Enter coefficient for term 1: 6
Enter exponent for term 1: 5
Enter coefficient for term 2: 7
Enter exponent for term 2: 8
Polynomial 1=(0x^0)+
Polynomial 2=(0x^0)+
Sum of the two polynomials is=(0x^0)
The present program code is-
poly *insert(poly*,poly*,poly *);
poly *input(poly *,poly *);
void display(poly *);
poly *insert(poly *new,poly *head,poly *last)
{
poly *p,*q;
if(head==NULL&&last==NULL)
{
head=last=new;
return;
}
p=head;
q=NULL;
while(new->exp<p->exp)
{
q=p;
p=p->next;
}
if(p->exp==new->exp)
p->coeff=p->coeff+new->coeff;
else
{
if(q!=NULL)
{
q->next=new;
new->next=p;
}
else if(q==NULL)
{
new->next=head;
head=new;
}
else if(p==NULL)
{
last->next=new;
last=new;
}
}
return head;
}
poly *input(poly *head,poly *last)
{
int i,n,c,e;
poly *new;
printf("How many no. terms do you want to enter? ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new=(poly *)malloc(sizeof(poly));
if(new==NULL)
{
printf("Allocation Error!!");
break;
}
printf("\nEnter coefficient for term %d: ",i);
scanf("%d",&new->coeff);
printf("Enter exponent for term %d: ",i);
scanf("%d",&new->exp);
new->next=NULL;
head=insert(new,head,last);
headC=insert(new,headC,lastC);
free(new);
}
return head;
}
void display(poly *start)
{
poly *p;
p=start;
while(p!=NULL)
{
printf("(%dx^%d)+",p->coeff,p->exp);
p=p->next;
}
printf("\b");
}
void main()
{
system("clear");
headA=(poly *)malloc(sizeof(poly));
headB=(poly *)malloc(sizeof(poly));
headC=(poly *)malloc(sizeof(poly));
lastA=(poly *)malloc(sizeof(poly));
lastB=(poly *)malloc(sizeof(poly));
lastC=(poly *)malloc(sizeof(poly));
if(headA==NULL||headB==NULL||headC==NULL||lastA==NULL||lastB==NULL||lastC==NULL)
{
printf("Allocation failure!!!");
return;
}
headA=headB=headC=NULL;
lastA=lastB=lastC=NULL;
printf("Polynomial 1\n\n");
headA=input(headA,lastA);
printf("\nPolynomial 2\n\n");
headB=input(headB,lastB);
printf("\n\nPolynomial 1=");
display(headA);
printf("\nPolynomial 2=");
display(headB);
printf("\nSum of the two polynomials is=");
display(headC);
}