Thread: Add two polynomials using an array

  1. #1
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79

    Add two polynomials using an array

    HI
    I want to add two polynomials
    5x^2 - 3xy + y
    and
    2x^2 - y^2 + 5xy - x + y

    using arrays .
    I used one array to store the coefficient,power of x ,power of y for the subsequent terms of the 1st polynomial.
    a=|5|2|0|-3|1|1|1|0|1|

    similary for the other polynomial the array is
    b=|2|2|0|-1|0|2|5|1|1|-1|1|0|1|0|1|

    Then I copied both the arrays into a third array
    c=|5|2|0|-3|1|1|1|0|1|2|2|0|-1|0|2|5|1|1|-1|1|0|1|0|1|

    so the 0,3,6,9....elements of array display the coeff.
    1,4,7....elements of array display the power of x
    2,5,8....elements of array display the power of y

    I traversed the array and added the coeff. of the terms having same power of x and y
    and then when I try to print the array it is not showing the proper result (showing only the sum of the highest terms)
    can anyone point out my mistake ?

    Code:
    #include <stdio.h>
    int main()
    {
        int a[27],b[27],c[54],m,n,i,j,z,y=0,t,s=0;
        printf ("How many terms you want to add in the 1st polynomial ?? : ");
        scanf ("%d",&n);
        
        printf ("Enter 1st polynomial : \n");
        for (i=0;i<n*3;i=i+3)
        {
            printf ("Enter coefficient : ");
            scanf("%d",&a[i]);
            printf ("Enter power of x : ");
            scanf("%d",&a[i+1]);
            printf ("Enter power of y : ");
            scanf("%d",&a[i+2]);
        }
        printf ("1st polynomial is : ");
        for (i=0;i<n*3;i=i+3)
        {
            printf ("(%dx^%dy^%d) + ",a[i],a[i+1],a[i+2]);
            
        }
        printf (" 0 \n");
        printf ("How many terms you want to add in the 2nd polynomial ?? : ");
        scanf ("%d",&m);
        
        printf ("Enter 2nd polynomial : \n");
        for (i=0;i<m*3;i=i+3)
        {
            printf ("Enter coefficient : ");
            scanf("%d",&b[i]);
            printf ("Enter power of x : ");
            scanf("%d",&b[i+1]);
            printf ("Enter power of y : ");
            scanf("%d",&b[i+2]);
        }
        printf ("2nd polynomial is : ");
        for (i=0;i<m*3;i=i+3)
        {
            printf ("(%dx^%dy^%d) + ",b[i],b[i+1],b[i+2]);
            
        }
        printf (" 0\n");
        
        
        printf ("Enter 1 to add  : ");
        scanf("%d",&z);
        switch (z)
        {
            case 1:
            for (i=0;i<n*3;i++)
            {c[i]=a[i];}
            for (i=n*3,j=0;i<(n+m)*3,j<m*3;i++,j++)
            {c[i]=b[j];}
            for (i=0;i<(m+n)*3;i=i+3)
        {
            printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);
            
        }
        printf (" 0\n");    
        }
        
        for (i=1;i<(m+n)*3;i=i+3)
        {
            for (j=4;j<(m+n)*3;j=j+3)
            {
                if (c[i]==c[j])
                { if(c[i+1]==c[j+1])
                {   
                    c[i-1]=c[i-1]+c[j-1];
                    c[j-1]=0;
    
    
                }
                }
            }
        }
        printf ("ADDITION \n");
        for (i=0;i<(m+n)*3;i=i+3)
        if (c[i]!=0)
        {
            printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);
            
        }
        else
        printf (" ");
    
    
        printf (" 0 \n");
        
    
    
    }

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  2. #2
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    5 hrs into the thread still no one replied

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by rajarshi View Post
    5 hrs into the thread still no one replied
    Probably something to do with the headache of looking at a jumble of a[i]b[j]c[i]c[j]m*3n*3(m+n)*3

    line 64-78. Think about how using nested for() loops is wrong here. You step through j, increment i, step through j, etc..

    Answer this and you've got it made... What distance separates the first and second polynomials paired data? That is, if I'm inpsecting the first polynomial's first coefficient, how far away is the second polynomial's matched coefficient?

  4. #4
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Tclausex View Post
    Probably something to do with the headache of looking at a jumble of a[i]b[j]c[i]c[j]m*3n*3(m+n)*3

    line 64-78. Think about how using nested for() loops is wrong here. You step through j, increment i, step through j, etc..

    Answer this and you've got it made... What distance separates the first and second polynomials paired data? That is, if I'm inpsecting the first polynomial's first coefficient, how far away is the second polynomial's matched coefficient?

    line 64-78 searches for the same powers of x and y in the array...
    and when the power matches.....it adds the coefficients to one place and leaves the coefficient of the other place as 0 ....

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials: Array of pointers to linked lists?
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 01:55 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. polynomials
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2008, 08:52 AM
  4. Factoring Polynomials in C++
    By MethSnax in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 01:51 PM
  5. polynomials
    By spinner in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 06:23 AM