-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpolyadd.c
105 lines (92 loc) · 2.32 KB
/
polyadd.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
void inputPoly(int, int[2][100]);
void printPoly(int, int[2][100]);
void polyAdd(int poly1[2][100], int poly2[2][100], int size1, int size2)
{
int sum[2][100], size3;
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2)
{
if (poly1[0][i] == poly2[0][j])
{
sum[0][k] = poly1[0][i];
sum[1][k] = poly1[1][i] + poly2[1][j];
i++;
j++;
}
else if (poly1[0][i] > poly2[0][j])
{
sum[0][k] = poly1[0][i];
sum[1][k] = poly1[1][i];
i++;
}
else
{
sum[0][k] = poly2[0][j];
sum[1][k] = poly2[1][j];
j++;
}
k++;
}
while (i < size1)
{
sum[0][k] = poly1[0][i];
sum[1][k] = poly1[1][i];
i++;
k++;
}
while (j < size2)
{
sum[0][k] = poly2[0][j];
sum[1][k] = poly2[1][j];
j++;
k++;
}
size3 = k;
printf("\nSum of Polynomials: ");
printPoly(size3, sum);
}
int main()
{
int poly1[2][100], poly2[2][100];
int size1, size2;
printf("\nEnter number of terms in first polynomial ");
scanf("%d", &size1);
if (size1 < 0 || size1 > 100)
{
printf("\nInvalid size or size exceeded max limit (100) ");
return 0;
}
inputPoly(size1, poly1);
printf("\nEnter number of terms in first polynomial ");
scanf("%d", &size2);
if (size2 < 0 || size2 > 100)
{
printf("\nInvalid size or size exceeded max limit (100) ");
return 0;
}
inputPoly(size2, poly2);
printf("\nFirst Polynomial: ");
printPoly(size1, poly1);
printf("\nSecond Polynomial: ");
printPoly(size2, poly2);
polyAdd(poly1, poly2, size1, size2);
}
void inputPoly(int size, int poly[2][100])
{
printf("\nEnter the coeffiecent and power of polynomial, in descending order of power");
printf("\nFor example: 3X^(5) + 4x^(2) + x + 6 is entered as 3 5 4 2 1 1 6 0\n");
for (int i = 0; i < size; i++)
{
scanf("%d", &poly[1][i]);
scanf("%d", &poly[0][i]);
}
}
void printPoly(int size, int poly[2][100])
{
for (int i = 0; i < size; i++)
{
printf(" %dx^(%d) ", poly[1][i], poly[0][i]);
i < size - 1 ? printf("+") : printf("");
}
}