-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly_multi.c
84 lines (75 loc) · 1.84 KB
/
poly_multi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <math.h>
#define MAX 50
struct poly {
int exp;
int coeff;
};
void main()
{
struct poly poly1[MAX], poly2[MAX], poly3[MAX];
int t1, t2, i, j, k;
printf("Enter the highest power of polynomial 1:\n");
scanf("%d", &t1);
printf("Enter the details of first polynomial:\n");
for (i = t1; i >= 0; i--)
{
printf("The coefficient of x^%d : ", i);
scanf("%d", &poly1[i].coeff);
poly1[i].exp = i;
}
printf("Enter the highest power of polynomial 2:\n");
scanf("%d", &t2);
printf("Enter the details of second polynomial:\n");
for (i = t2; i >= 0; i--)
{
printf("The coefficient of x^%d : ", i);
scanf("%d", &poly2[i].coeff);
poly2[i].exp = i;
}
// Initialize poly3 coefficients to 0.
for (i = 0; i < MAX; i++)
{
poly3[i].coeff = 0;
poly3[i].exp = 0;
}
// Perform polynomial multiplication
for (i = 0; i <= t1; i++)
{
for (j = 0; j <= t2; j++)
{
k = poly1[i].exp + poly2[j].exp;
poly3[k].coeff += poly1[i].coeff * poly2[j].coeff;
poly3[k].exp = k;
}
}
// Determine the highest power of the resulting polynomial
int max_power = 0;
for (i = 0; i < MAX; i++)
{
if (poly3[i].coeff != 0)
{
max_power = poly3[i].exp;
}
}
printf("Resultant polynomial is :\n");
for (i = max_power; i >= 0; i--)
{
if (poly3[i].coeff != 0)
{
if (i == 0)
{
printf("%d", poly3[i].coeff);
}
else
{
printf("%dx^%d ", poly3[i].coeff, poly3[i].exp);
}
if (i > 0)
{
printf("+ ");
}
}
}
printf("\n");
}