Addition and substraction of polynomials is working fine but the multiplication isn't
#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[i]=0;
}
void add(int p1[],int p2[],int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3[i]=p1[i] + p2[i];
}
void sub(int p1[],int p2[],int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3[i]=p1[i] - p2[i];
}
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[i]*p2[j];
}
void print(int p[])
{
int i;
for(i=0;i<MAX;i++)
if(p[i]!=0)
printf("%dX^%d ",p[i],i);
}
ybee [#5]The no. of C users π€ the no. of bitches I get
(0)
Ikr? But I have to do it for the university assignments :β ^β )
capital_d_colon [#7]like not running or output wrong
It's running but it's giving garbage or random values upto 20th term
FrozenFlame [#8]It's running but it's giving garbage or random values upto 20th term
ok i have never done c but the issue is to do with looping through the entire array for p1 or p2
If you set the arrays to a set value it doesnt error out
probably something to do with trying to do multiplication on an empty number
p1[] = {values}
or
p1[number of terms];
FrozenFlame [#2]#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[i]=0;
}
void add(int p1[],int p2[],int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3[i]=p1[i] + p2[i];
}
void sub(int p1[],int p2[],int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3[i]=p1[i] - p2[i];
}
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[i]*p2[j];
}
void print(int p[])
{
int i;
for(i=0;i<MAX;i++)
if(p[i]!=0)
printf("%dX^%d ",p[i],i);
}
Vro copy pasted from W3schools