-
Notifications
You must be signed in to change notification settings - Fork 2
/
PolynomialAddition.c
84 lines (82 loc) · 1.97 KB
/
PolynomialAddition.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
/*
Program for adding two polynomials using array
Author: Vivek K J
S3 CSA
*/
#include<stdio.h>
// Structure for polynomial
struct polynomial{
int coeff;
int exp;
};
void main(){
int N1, N2, i, j, k;
// Read First Polynomial
printf("Enter no. of terms of first polynomial:");
scanf("%d", &N1);
struct polynomial first[N1];
for(i=0; i<N1; i++){
printf("Enter coefficient of term %d:", i+1);
scanf("%d", &first[i].coeff);
printf("Enter exponent of term %d:", i+1);
scanf("%d", &first[i].exp);
}
// Read Second Polynomial
printf("Enter no. of terms of second polynomial:");
scanf("%d", &N2);
struct polynomial second[N2];
for(i=0; i<N2; i++){
printf("Enter coefficient of term %d:", i+1);
scanf("%d", &second[i].coeff);
printf("Enter exponent of term %d:", i+1);
scanf("%d", &second[i].exp);
}
// Print both Polynomials
printf("Polynomials are: \n");
printf("First Polynomial:");
for(i=0; i<N1; i++){
printf("%+dx^%d", first[i].coeff, first[i].exp);
}
printf("\n");
printf("Second Polynomial:");
for(i=0; i<N2; i++){
printf("%+dx^%d", second[i].coeff, second[i].exp);
}
printf("\n");
// Create a structure variable for result with max size of N1+N2
struct polynomial result[N1+N2];
// Actual Polynomial Addition
i=0, j=0, k=0;
while(i<N1 && j<N2){
if(first[i].exp == second[j].exp){
result[k].coeff = first[i].coeff + second[j].coeff;
result[k].exp = first[i].exp;
i++, j++, k++;
}
else if(first[i].exp > second[j].exp){
result[k].coeff = first[i].coeff;
result[k].exp = first[i].exp;
i++, k++;
}
else{
result[k].coeff = second[j].coeff;
result[k].exp = second[j].exp;
j++, k++;
}
}
while(i<N1){
result[k].coeff = first[i].coeff;
result[k].exp = first[i].exp;
i++, k++;
}
while(j<N2){
result[k].coeff = second[j].coeff;
result[k].exp = second[j].exp;
j++, k++;
}
// Print resultant polynomial
printf("Resultant Polynomial Is: ");
for(i=0; i<k; i++){
printf("%+dx^%d", result[i].coeff, result[i].exp);
}
}