# Thread: Add two polynomials using an array

1. ## 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;

}
}
}
}
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");

}```

2. 5 hrs into the thread still no one replied

3. Originally Posted by rajarshi
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. Originally Posted by Tclausex
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 ....

Popular pages Recent additions