Language - C
Compiler - Netbeans, MSYS
Statement - I take two poly from user, ask user for its choice of multiplication. Multiplication takes as expected, with same results as on normal paper.
But in Multiplication function, I am calling finalise func:- so to add coeff of same exponent which were generated after multiplication.
*first - holds add of first poly
- sec - holds add of second poly
the notation I am using is x^2 = x square. i.e x2
Poly 1: (5x^3+2x^2+3x^1)
Poly 2: (4x^3+5x^2+6x^1)
multiplication result = 20x^6+25x^5+30x^4+8x^5+10x^4+12x^3+12x^4+15x^3+18x^2 - As expected, I print this on console. Stored in *new
In finalise - I try to add coeff of same exp… and expecting asnwer as
20x^6 33x^5 52x^4 27x^3 18x^2 (ignored + sign )
however, I get result as:
20x^6 33x^5 52x^4 4985160x^4985088 10x^4 27x^3 4985018x^4984768 15x^3 18x^2
My modus operandi - in multiplication result. I am deleting that comp(*f) which matches with loop’s variable after addition to loop’s (*temp) variable.
temp points to *new. And f is next node of temp.
What I have tried is: 20x^6+25x^5+30x^4+8x^5+10x^4+12x^3+12x^4+15x^3+18x^2
when temp points to 30x^4… and adds 10x^4… I delete that 10x^4, and check if my f pointer points to next as expected 12x^3 node or node.
Also, I am checking which node it is deleting by verifying coeff & exponent.
I am unable to figure out where, the bug is. There is prob while I free the node and link f to next. But that again looks ok to me when I print the same in console.
I am new to C programming and also, don’t know how to debug program in Netbeans.
Any help shall be appreciated.
Thanks much in advance.
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int coeff;
int exp;
struct node *link;
}*first=NULL,*sec=NULL,*tot=NULL,*new=NULL ;
void appendF(){
struct node *temp;
temp=first;
int cof,expo;
printf("\nenter coeff for first poly\n");
scanf("%d",&cof);
printf("\nenter expo for first poly\n");
scanf("%d",&expo);
if(temp==NULL){
temp=(struct node*)malloc(sizeof(struct node));
temp->coeff=cof;
temp->exp=expo;
temp->link=NULL;
first=temp;
return;
}
else{
temp=first;
while(temp->link!=NULL){
temp=temp->link;
}
temp=temp->link=(struct node*)malloc(sizeof(struct node));
temp->coeff=cof;
temp->exp=expo;
temp->link=NULL;
}
return;
};
void appendS(){
struct node *temp;
temp=sec;
int cof,expo;
printf("\nenter coeff for second poly\n");
scanf("%d",&cof);
printf("\nenter expo for second poly\n");
scanf("%d",&expo);
if(temp==NULL){
temp=(struct node*)malloc(sizeof(struct node));
temp->coeff=cof;
temp->exp=expo;
temp->link=NULL;
sec=temp;
return;
}
else{
temp=sec;
while(temp->link!=NULL){
temp=temp->link;
}
temp=temp->link=(struct node*)malloc(sizeof(struct node));
temp->coeff=cof;
temp->exp=expo;
temp->link=NULL;
}
return;
};
void disp(){
struct node *temp;
temp=first;
while(temp!=NULL){
printf(" %dx^%d :",temp->coeff,temp->exp);
temp=temp->link;
}
return;
};
mult(){
struct node *pF, *qS, *temp,*third;
pF=first;
qS=sec;
while(pF!=NULL){
qS=sec;
while(qS!=NULL){
third=(struct node*)malloc(sizeof(struct node));
third->link=NULL;
third->coeff=(pF->coeff)*(qS->coeff);
third->exp=pF->exp+qS->exp;
if(new==NULL){
new=third;
}
else{
temp=new;
while(temp->link!=NULL){
temp=temp->link;
}
temp->link=third;
}
qS=qS->link;
}
pF=pF->link;
}
finalise(&new);
};
finalise(struct node **h){
struct node *temp,*p,*f;
int flag=0;
temp=*h;
printf("\n\n");
while(temp!=NULL){
printf(" %dx^%d ",temp->coeff,temp->exp);
temp=temp->link;
}
temp=*h;
while(temp->link!=NULL){
f=temp->link;
while(f->link!=NULL){
flag=0;
if(temp->exp==f->exp){
flag=1;
temp->coeff=temp->coeff+f->coeff;
p=f;
f=f->link;
printf("\nNext comp is %dx^%d",f->coeff,f->exp);
printf("\nThe free is %dx^%d",p->coeff,p->exp);
free(p);
}
if(flag==0){
f=f->link;}
}
temp=temp->link;
}
printf("\nAgain\n");
temp=*h;
printf("\n\n");
while(temp!=NULL){
printf(" %dx^%d ",temp->coeff,temp->exp);
temp=temp->link;
}
return;
};
void disV(struct node **q){
struct node *temp;
temp=*q;
while(temp!=NULL){
printf(" %dx^%d +",temp->coeff,temp->exp);
temp=temp->link;
}
return;
};
int main() {
int c;
do{
printf("\n1 append First 2-Display 3-Append-Second 4 exit 5-Add 6-Display via address\n");
printf("\n7-Add by my method 8-Multi\n");
scanf("%d",&c);
switch(c){
case 1:
appendF();
break;
case 2:
disp();
break;
case 3:
appendS();
break;
case 4:
printf("\nProgram ends\n");
break;
case 5:
add();
break;
case 6:
disV(&new);
break;
case 7:
addD();
break;
case 8:
mult();
break;
default:
printf("\nWrong choice\n");
}
}while(c!=4);
return 0;
}