Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12921 Members
Join this group to post and comment.
samaira • Oct 4, 2009

# problem in a program

hey the following c code executes properly except case 4.This is the code for polynomial operations using linked list.So can anyone tell me my mistake😔

#include
#include
#include
#include
int power(int x,int y);
struct pnode
{
int coeff;
int pow;
void main()
{
int sum,a,b,x,y,ch,n,m,i;
do
{
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the no of terms in the first polynomial: ");
scanf("%d",&n);
for(i=0;i {
printf("\n enter the coeff and the pow: ");
scanf("%d%d",&a,&b);
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=a;
newnode->pow=b;
{
prev=newnode;
}
else
{
prev=newnode;
}
}
break;
case 2:
printf("\n enter the no of terms in the second polynomial: ");
scanf("%d",&m);
for(i=0;i {
printf("\n enter the coeff and the pow: ");
scanf("%d%d",&a,&b);
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=a;
newnode->pow=b;
{
prev=newnode;
}
else
{
prev=newnode;
}
}
break;
case 3:
printf("\n the first polynomial is: ");
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
}
printf("\n the 2nd polynomial is: ");
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
}
break;
case 4:
{
while(temp!=NULL)
{
{
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=x;
newnode->pow=y;
{
prev=newnode;
}
else
{
prev=newnode;
}
}
else
{
}
}
if(temp==NULL)
{
newnode=(struct pnode*)malloc(sizeof(struct pnode));
{
prev=newnode;
}
else
{
prev=newnode;
}
}
}
{
while(temp!=NULL)
{
{
break;
}
else
{
}
}
if(temp==NULL)
{
newnode=(struct pnode*)malloc(sizeof(struct pnode));
{
}
else
{
prev=newnode;
}
}
}
printf("\n the addition of polynomials is: ");
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
}
break;
case 5:
{
{
x=temp1->coeff*temp2->coeff;
y=temp1->pow+temp2->pow;
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=x;
newnode->pow=y;
{
prev=newnode;
}
else
{
while(temp3!=NULL)
{
if(temp3->pow==y)
{
temp3->coeff=temp3->coeff+x;
break;
}
else
{
}
}
if(temp3==NULL)
{
prev=newnode;
}
}
}
}
printf("\n the multiplication of polynomials is: ");
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
}
break;
case 6:
printf("\n enter the value of x: ");
scanf("%d",&x);
sum=0;
{
sum=sum+temp1->coeff*power(x,temp1->pow);
}
printf("\n the result after evaluation is: %d",sum);
break;
}
}while(ch<7);
}
int power(int x,int y)
{
int i,p=1;
for(i=0;i {
p=x*p;
}
return p;
}