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

Joined
Nov 22, 2022
Messages
1
Reaction score
0
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 20
void init(int p[]);
void read(int p1[],int p2[]);
void print(int p[]);
void add(int p1[],int p2[],int p3[]);
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 ");
printf("\n 1. Please enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1 : read(p1,p2);
break;
case 2 : add(p1,p2,p3);
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);
}
void read(int p1[],int p2[])
{
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;
}
void add(int p1[],int p2[],int p3[])
{
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);
}
 
Joined
Sep 21, 2022
Messages
114
Reaction score
14
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 20
void init(int p[]);
void read(int p1[],int p2[]);
void print(int p[]);
void add(int p1[],int p2[],int p3[]);
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 ");
printf("\n 1. Please enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1 : read(p1,p2);
break;
case 2 : add(p1,p2,p3);
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);
}
void read(int p1[],int p2[])
{
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;
}
void add(int p1[],int p2[],int p3[])
{
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]?
}
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top