-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomialSum_Arrays.c
99 lines (89 loc) · 2.6 KB
/
polynomialSum_Arrays.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
// IMPLEMENTING POLYNOMIAL SUM
// POLYNOMIAL REPRESENTED IN STRUCT ARRAY
#include<stdio.h>
typedef struct{
int expon;
float coef;
}polynomial;
polynomial terms[100];
int avail = 0;
int polynomialZero(){
return avail;
}
int takeInput(int n){
printf(" (Enter the values in the decreasing degree)\n");
for(int i=0;i<n;i++){
printf("\nexponent value: ");
scanf("%d",&terms[avail].expon);
printf("coefficient value: ");
scanf("%f",&terms[avail].coef);
avail++;
}
printf("\n");
return avail-1;
}
int addAB(int startC,int startA,int finishA,int startB,int finishB){
int i=startA,j=startB,k=startC;
while(i<=finishA && j<=finishB){
if (terms[i].expon==terms[j].expon){
terms[k].expon = terms[i].expon;
terms[k].coef = terms[i].coef+terms[j].coef;
j++ ; i++ ;
}
else if(terms[i].expon>terms[j].expon){
terms[k].expon = terms[i].expon;
terms[k].coef = terms[i].coef;
i++;
}
else{
terms[k].expon = terms[j].expon;
terms[k].coef = terms[j].coef;
j++;
}
k++;
}
for(;i<=finishA;i++,k++){
terms[k].expon = terms[i].expon;
terms[k].coef = terms[i].coef;
}
for(;j<=finishB;j++,k++){
terms[k].expon = terms[j].expon;
terms[k].coef = terms[j].coef;
}
return k-1;
avail = k;
}
void display(int start, int finish){
for (int i=start;i<finish;i++){
if (terms[i].expon!=0)
printf("%.2fx^%d + ",terms[i].coef,terms[i].expon);
else
printf("%.2fx + ",terms[i].coef);
}
if (terms[finish].expon==0)
printf("%.2f\n",terms[finish].coef);
else
printf("%.2fx^%d\n",terms[finish].coef,terms[finish].expon);
}
void main(){
int n1,n2,startA,startB,finishA,finishB,startC,finishC;
printf("Enter the number of terms in polynomial A:");
scanf("%d",&n1);
startA=polynomialZero();
printf("Enter polynomial A");
finishA=takeInput(n1);
printf("Enter the number of terms in polynomial B:");
scanf("%d",&n2);
startB=polynomialZero();
printf("Enter polynomial B");
finishB=takeInput(n2);
startC=polynomialZero();
finishC=addAB(startC,startA,finishA,startB,finishB);
printf("\nPOLYNOMIAL A\n");
display(startA,finishA);
printf("\nPOLYNOMIAL B\n");
display(startB,finishB);
printf("\nPOLYNOMIAL C=(A+B)\n");
display(startC,finishC);
printf("\n");
}