-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoliadd.c
More file actions
64 lines (62 loc) · 1.33 KB
/
Copy pathpoliadd.c
File metadata and controls
64 lines (62 loc) · 1.33 KB
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
#include<stdio.h>
void polyadd(int poly1[],int poly2[],int exp1[],int exp2[],int size1,int size2,int expresult[],int result[]){
int i=0,j=0,k=0;
while(i<size1&&j<size2){
if(exp1[i]>exp2[j]){
result[k]=poly1[i];
expresult[k]=exp1[i];
i++;
}
else if(exp1[i]<exp2[j]){
result[k]=poly2[j];
expresult[k]=exp2[j];
j++;
}
else{
result[k]=poly1[i]+poly2[j];
expresult[k]=exp1[i];
i++;
j++;
}
k++;
}
while(i<size1){
result[k]=poly1[i];
expresult[k]=exp1[i];
i++;
k++;
}
while(j<size2){
result[k]=poly2[j];
expresult[k]=exp2[j];
j++;
k++;
}
printf("polynomial representation: ");
for(i=0;i<k;i++){
printf("%dx^%d",result[i],expresult[i]);
if(i!=k-1){
printf("+");
}
}
printf("\n");
}
int main(){
int n,i,size1,size2;
printf("enter the size of the poly1: ");
scanf("%d",&size1);
int poly1[size1],exp1[size1];
printf("enter the terms (coeff expo): ");
for(i=0;i<size1;i++){
scanf("%d%d",&poly1[i],&exp1[i]);
}
printf("enter the size of poly2: ");
scanf("%d",&size2);
int poly2[size2],exp2[size2];
printf("enter the terms (coeff expo): ");
for(i=0;i<size2;i++){
scanf("%d%d",&poly2[i],&exp2[i]);
}
int result[size1+size2],expresult[size1+size2];
polyadd(poly1,poly2,exp1,exp2,size1,size2,expresult,result);
}