# Addition and substraction of polynomials is working fine but the multiplication isn't; what's wrong with my code

#### FrozenFlame

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 20
void init(int p[]);
void print(int p[]);
void sub(int p1[],int p2[],int p3[]);
void multiply(int p1[],int p2[],int p3[]);
void main()
{
int p1[MAX],p2[MAX],p3[MAX];
int choice;
do
{
printf("\n 1. Creation of polynomials ");
printf("\n 2. Addition of polynomials ");
printf("\n 3. Substraction of polynomials ");
printf("\n 4. Multiplication of polynomials ");
printf("\n 5. EXIT ");
scanf("%d",&choice);
switch(choice)
{
break;
printf("\n 1st Polynomial ");
print(p1);
printf("\n 2nd Polynomial ");
print(p2);
printf("\n Sum of given polynomials : ");
print(p3);
break;
case 3 : sub(p1,p2,p3);
printf("\n 1st Polynomial ");
print(p1);
printf("\n 2nd Polynomial ");
print(p2);
printf("\n Substraction of given polynomials : ");
print(p3);
break;
case 4 : multiply(p1,p2,p3);
printf("\n 1st Polynomial ");
print(p1);
printf("\n 2nd Polynomial ");
print(p2);
printf("\n Multiplication of given polynomials : ");
print(p3);
break;
}
}
while(choice!=5);
}
{
int n,i,power1,coeff1,power2,coeff2;
init(p1);
printf("\n Enter number of terms : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the First term (power,coefficient) ");
scanf("%d%d",&power1,&coeff1);
p1[power1]=coeff1;
}
init(p2);
printf("\n Enter number of terms : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Second term (power,coefficient) ");
scanf("%d%d",&power2,&coeff2);
p2[power2]=coeff2;
}

}
void init(int p[])
{
int i;
for(i=0;i<MAX;i++)
p=0;
}
{
int i;
for(i=0;i<MAX;i++)
p3=p1 + p2;
}
void sub(int p1[],int p2[],int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3=p1 - p2;
}
void multiply(int p1[],int p2[],int p3[])
{
int i,j,k;
for( i=0; i<=MAX; i++)
for( j=0; j<=MAX; j++)
k=i+j;
p3[k] = p3[k]+p1*p2[j];
}
void print(int p[])
{
int i;
for(i=0;i<MAX;i++)
if(p!=0)
printf("%dX^%d ",p,i);
}

#### WhiteCube

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 20
void init(int p[]);
void print(int p[]);
void sub(int p1[],int p2[],int p3[]);
void multiply(int p1[],int p2[],int p3[]);
void main()
{
int p1[MAX],p2[MAX],p3[MAX];
int choice;
do
{
printf("\n 1. Creation of polynomials ");
printf("\n 2. Addition of polynomials ");
printf("\n 3. Substraction of polynomials ");
printf("\n 4. Multiplication of polynomials ");
printf("\n 5. EXIT ");
scanf("%d",&choice);
switch(choice)
{
break;
printf("\n 1st Polynomial ");
print(p1);
printf("\n 2nd Polynomial ");
print(p2);
printf("\n Sum of given polynomials : ");
print(p3);
break;
case 3 : sub(p1,p2,p3);
printf("\n 1st Polynomial ");
print(p1);
printf("\n 2nd Polynomial ");
print(p2);
printf("\n Substraction of given polynomials : ");
print(p3);
break;
case 4 : multiply(p1,p2,p3);
printf("\n 1st Polynomial ");
print(p1);
printf("\n 2nd Polynomial ");
print(p2);
printf("\n Multiplication of given polynomials : ");
print(p3);
break;
}
}
while(choice!=5);
}
{
int n,i,power1,coeff1,power2,coeff2;
init(p1);
printf("\n Enter number of terms : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the First term (power,coefficient) ");
scanf("%d%d",&power1,&coeff1);
p1[power1]=coeff1;
}
init(p2);
printf("\n Enter number of terms : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Second term (power,coefficient) ");
scanf("%d%d",&power2,&coeff2);
p2[power2]=coeff2;
}

}
void init(int p[])
{
int i;
for(i=0;i<MAX;i++)
p=0;
}
{
int i;
for(i=0;i<MAX;i++)
p3=p1 + p2;
}
void sub(int p1[],int p2[],int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3=p1 - p2;
}
void multiply(int p1[],int p2[],int p3[])
{
int i,j,k;
for( i=0; i<=MAX; i++)
for( j=0; j<=MAX; j++)
k=i+j;
p3[k] = p3[k]+p1*p2[j];
}
void print(int p[])
{
int i;
for(i=0;i<MAX;i++)
if(p!=0)
printf("%dX^%d ",p,i);
}
Code:
``````void multiply(int p1[],int p2[],int p3[])
{
int i,j,k;
for( i=0; i<=MAX; i++)
for( j=0; j<=MAX; j++)
k=i+j; What happens when k > MAX?
p3[k] = p3[k]+p1*p2[j]; Is this statement inside the loop? p1 or p1[i]?
}``````

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.