Polynomial Multiplication program

This is a discussion on Polynomial Multiplication program within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> typedef struct poly_node { int coeff; int power; struct poly_node *next; }polynode; polynode* create(polynode*,int); void polymulti(polynode*,polynode*); ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Polynomial Multiplication program

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    typedef struct poly_node
    {
        int coeff;
        int power;
        struct poly_node *next;
    }polynode;
    polynode* create(polynode*,int);
    
    void polymulti(polynode*,polynode*);
    void display(polynode *,polynode *,polynode*);
    int main()
    {
              int n1,n2;
              polynode *head1,*head2;
              head1=(polynode*)malloc(sizeof(polynode));
              printf("\n Enter Elements in first List \n");
              printf("Enter no. of elements of 1st list = ");
              scanf("%d",&n1);
          printf("Enter coefficient of head1 = ");
              scanf("%d",&head1->coeff);
              printf("Enter power of head1 = ");
          scanf("%d",&head1->power);
        head1->next=NULL;
        head1=create(head1,n1);
        
        
        head2=(polynode*)malloc(sizeof(polynode));
        printf("\n Enter Elements in Second List \n");
        printf("Enter no. of elements of 2nd list = ");
        scanf("%d",&n2);
        printf("Enter coefficient of head2 = ");
        scanf("%d",&head2->coeff);
        printf("Enter power of head2 = ");
        scanf("%d",&head2->power);
        head2->next=NULL;
        head2=create(head2,n2);
        polymulti(head1,head2);
        return 0;
    }
    polynode *create(polynode *head,int n)
    {
        polynode *new,*q;
        int i;
        q=head;
        for(i=1;i<n;i++)
        {
            new=(polynode*)malloc(sizeof(polynode));
            printf("Enter coefficient = ");
            scanf("%d",&new->coeff);
            printf("Enter power = ");
            scanf("%d",&new->power);
            new->next=NULL;
            q->next=new;
            q=new;
        }
        return head;
    }
    void polymulti(polynode *head1,polynode *head2)
    {
        polynode *p,*q,*result,*s;
        result=NULL;
        int newcoeff,newpower,i;
        p=head1;
        q=head2;
        
        while(p!=NULL)
        {
                while(q!=NULL)
                {
                    newcoeff=(p->coeff)*(q->coeff);
                    newpower=(p->power)+(q->power);
                    s=(polynode*)malloc(sizeof(polynode));
                    s->coeff=newcoeff;
                    s->power=newpower;
                    s->next=NULL;
                    if(result==NULL)
                          result=s;
                    else
                          result->next=s;
                    q=q->next;            
                }
            p=p->next;
        }
        display(head1,head2,result);
    }
    void display(polynode *head1,polynode *head2,polynode *result)
    {
            polynode *p;
            printf("\n PRINTING THE FIRST LIST : \n");
            p=head1;
            while(p!=NULL)
            {
                  printf("%d--%d\t",p->coeff,p->power);
                  p=p->next;
            }
            p=head2;
            printf("\n PRINTING THE SECOND LIST : \n");
            while(p!=NULL)
            {
                  printf("%d--%d\t",p->coeff,p->power);
                  p=p->next;
        }
        printf("\n PRINTING THE RESULTANT LIST : \n");
            p=result;
            while(p!=NULL)
            {
                  printf("%d--%d\t",p->coeff,p->power);
                  p=p->next;
            }
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Suppose the 1st list is 3--2 5--2 2--0 and the second list is 9--2 4--1 3--0, then this code prints the output as follows :
    27--4 9--2

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Consider something like this, to cut down on the copy/pasting of code.
    Code:
    int main ( ) {
        polynode *head1,*head2,*result;
        head1 = create();
        head2 = create();
        print(head1);  // for debug
        print(head2);
        result = mult(head1,head2);
        print(result);
        destroy(head1);
        destroy(head2);
        destroy(result);
        return 0;
    }
    polynode *create ( ) {
        polynode *result = NULL;
        printf("How many nodes");
        for ( i = 0 ; i < n ; i++ ) {
            printf("Input power and coefficient");
            result = append(result,power,coefficient);
        }
        return result;
    }
    polynode *append ( polynode *head, int power, int coeff ) {
        // allocate new node
        // append to head
        // return new head
        // hint: head only changes once (when it's NULL initially)
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomial program, Unknown Error.
    By protofarmer720 in forum C++ Programming
    Replies: 5
    Last Post: 06-14-2011, 01:24 AM
  2. Issue with Horner's polynomial c program
    By swebdev in forum C Programming
    Replies: 6
    Last Post: 05-24-2011, 12:13 PM
  3. polynomial multiplication using bitwise operators
    By arian23 in forum C Programming
    Replies: 8
    Last Post: 04-28-2010, 03:54 PM
  4. Pls help with mulvariate polynomial multiplication in C
    By aspk2000 in forum C Programming
    Replies: 1
    Last Post: 03-15-2010, 05:33 AM
  5. Is it just me, or is the polynomial program popular?
    By CaptainMorgan in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-05-2006, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21