CrazyEngineers
  • problem in a program

    samaira

    samaira

    @samaira-P4RsEf
    Updated: Oct 23, 2024
    Views: 983
    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<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<math.h>
    int power(int x,int y);
    struct pnode
    {
    int coeff;
    int pow;
    struct pnode *link;
    }*temp1,*temp2,*temp3,*head1,*head2,*head3,*temp,*prev,*newnode;
    void main()
    {
    int sum,a,b,x,y,ch,n,m,i;
    do
    {
    printf("\n linked list operations\n1.creation of the 1st polynomials\n2.creation of the second polynomial\n3.display\n4.addition\n5.multiplication\n6.\evaluation\nenter your choice: ");
    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<n;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;
    newnode->link=NULL;
    if(head1==NULL)
    {
    head1=newnode;
    prev=newnode;
    }
    else
    {
    prev->link=newnode;
    prev=newnode;
    }
    }
    break;
    case 2:
    printf("\n enter the no of terms in the second polynomial: ");
    scanf("%d",&m);
    for(i=0;i<m;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;
    newnode->link=NULL;
    if(head2==NULL)
    {
    head2=newnode;
    prev=newnode;
    }
    else
    {
    prev->link=newnode;
    prev=newnode;
    }
    }
    break;
    case 3:
    printf("\n the first polynomial is: ");
    temp=head1;
    while(temp!=NULL)
    {
    printf("%dx^%d+",temp->coeff,temp->pow);
    temp=temp->link;
    }
    printf("\n the 2nd polynomial is: ");
    temp=head2;
    while(temp!=NULL)
    {
    printf("%dx^%d+",temp->coeff,temp->pow);
    temp=temp->link;
    }
    break;
    case 4:
    while(head1!=NULL)
    {
    temp=head2;
    while(temp!=NULL)
    {
    if(head1->pow==temp->pow)
    {
    x=head1->coeff+temp->coeff;
    y=head1->pow;
    newnode=(struct pnode*)malloc(sizeof(struct pnode));
    newnode->coeff=x;
    newnode->pow=y;
    newnode->link=NULL;
    if(head3==NULL)
    {
    head3=newnode;
    prev=newnode;
    }
    else
    {
    prev->link=newnode;
    prev=newnode;
    }
    }
    else
    {
    temp=temp->link;
    }
    }
    if(temp==NULL)
    {
    newnode=(struct pnode*)malloc(sizeof(struct pnode));
    newnode->link=NULL;
    newnode=head1;
    if(head3==NULL)
    {
    head3=newnode;
    prev=newnode;
    }
    else
    {
    prev->link=newnode;
    prev=newnode;
    }
    }
    head1=head1->link;
    }
    while(head2!=NULL)
    {
    temp=head1;
    while(temp!=NULL)
    {
    if(head2->pow==temp->pow)
    {
    break;
    }
    else
    {
    temp=temp->link;
    }
    }
    if(temp==NULL)
    {
    newnode=(struct pnode*)malloc(sizeof(struct pnode));
    newnode->link=NULL;
    newnode=head2;
    if(head3==NULL)
    {
    prev=head3=newnode;
    }
    else
    {
    prev->link=newnode;
    prev=newnode;
    }
    }
    head2=head2->link;
    }
    printf("\n the addition of polynomials is: ");
    temp=head3;
    while(temp!=NULL)
    {
    printf("%dx^%d+",temp->coeff,temp->pow);
    temp=temp->link;
    }
    break;
    case 5:
    for(temp1=head1;temp1!=NULL;temp1=temp1->link)
    {
    for(temp2=head2;temp2!=NULL;temp2=temp2->link)
    {
    x=temp1->coeff*temp2->coeff;
    y=temp1->pow+temp2->pow;
    newnode=(struct pnode*)malloc(sizeof(struct pnode));
    newnode->coeff=x;
    newnode->pow=y;
    newnode->link=NULL;
    if(head3==NULL)
    {
    head3=newnode;
    prev=newnode;
    }
    else
    {
    temp3=head3;
    while(temp3!=NULL)
    {
    if(temp3->pow==y)
    {
    temp3->coeff=temp3->coeff+x;
    break;
    }
    else
    {
    temp3=temp3->link;
    }
    }
    if(temp3==NULL)
    {
    prev->link=newnode;
    prev=newnode;
    }
    }
    }
    }
    printf("\n the multiplication of polynomials is: ");
    temp=head3;
    while(temp!=NULL)
    {
    printf("%dx^%d+",temp->coeff,temp->pow);
    temp=temp->link;
    }
    break;
    case 6:
    printf("\n enter the value of x: ");
    scanf("%d",&x);
    sum=0;
    for(temp1=head1;temp1!=NULL;temp1=temp1->link)
    {
    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<y;i++)
    {
    p=x*p;
    }
    return p;
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register