This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathpolynomialsum.c
More file actions
85 lines (85 loc) · 1.87 KB
/
polynomialsum.c
File metadata and controls
85 lines (85 loc) · 1.87 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*Representation of polynomial and summation of two polynomials*/
#include <stdio.h>
typedef struct
{
int exp;
float coeff;
}terms;
void inputPoly(terms a[],int n)
{
printf("Please enter the input in descending order of degrees \n");
for(int i=0;i<n;i++)
{
printf("Enter the coefficient ");
scanf("%f",&a[i].coeff);
printf("Enter the exponent ");
scanf("%d",&a[i].exp);
}
}
displayPoly(terms a[],int n)
{
printf("%fx^%d",a[0].coeff,a[0].exp);
for(int i=1;i<n;i++)
{
printf("+%fx^%d",a[i].coeff,a[i].exp);
}
}
int polyAdd(terms a[],terms b[],terms c[],int n1,int n2)
{
int i=0,j=0,k=0;
while(i<=n1&&j<=n2)
{
if(a[i].exp==b[j].exp)
{
c[k].exp=a[i].exp;
c[k].coeff=a[i].coeff+b[j].coeff;
i++;j++;k++;
}
else if(a[i].exp>b[j].exp)
{
c[k].exp=a[i].exp;
c[k].coeff=a[i].coeff;
i++;k++;
}
else if(a[i].exp<b[j].exp)
{
c[k].exp=b[j].exp;
c[k].coeff=b[j].coeff;
j++;k++;
}
}
while(i<=n1)
{
c[k].exp=a[i].exp;
c[k].coeff=a[i].coeff;
i++;k++;
}
while(j<=n2)
{
c[k].exp=b[j].exp;
c[k].coeff=b[j].coeff;
j++;k++;
}
return k;
}
void main()
{
terms a[10],b[10],r[20];
int n1,n2;
printf("Enter number of terms in first polynomial\n");
scanf("%d",&n1);
printf("Enter number of terms in second polynomial\n");
scanf("%d",&n2);
printf("Enter first polynomial\n");
inputPoly(a,n1);
printf("Enter second polynomial\n");
inputPoly(b,n2);
printf("first polynomial\n");
displayPoly(a,n1);
printf("\n");
printf("second polynomial\n");
displayPoly(b,n2);
int k=polyAdd(a,b,r,n1,n2);
printf("\n");
displayPoly(r,k-1);
}