forked from adhilsalim/CST201
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolAdd.c
125 lines (101 loc) · 3.18 KB
/
PolAdd.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
name: PolAdd
desc: This program performs addition on polynomial expression
author: Adhil Salim, Abhishek P Anil, Advaith Manoj
date: 05/10/2022 (last modified)
Salim annan uyir
*/
#include <stdio.h>
void main()
{
int totalPolEqns = 0, totalTerms = 0;
// getting the number of polynomial equations
printf("Enter the number of polynomial equations: ");
scanf("%d", &totalPolEqns);
// for storing the number of terms in each polynomial equation
int totalTermsInEachPol[totalPolEqns];
// getting the number of terms in each polynomial equation
for (int i = 0; i < totalPolEqns; i++)
{
printf("Enter the number of terms in polynomial equation %d: ", i + 1);
scanf("%d", &totalTermsInEachPol[i]);
//
totalTerms += totalTermsInEachPol[i];
}
// array that contains coefficient and exponent of each term of all polynomial equations
int mainPol[totalTerms][2];
// getting the coefficient and exponent of each term of all polynomial equations
for (int i = 0, k = 0; i < totalPolEqns; i++)
{
while (totalTermsInEachPol[i] != 0)
{
printf("Enter the coefficient of term %d of polynomial equation %d: ", totalTermsInEachPol[i], i + 1);
scanf("%d", &mainPol[k][0]);
printf("Enter the exponent of term %d of polynomial equation %d: ", totalTermsInEachPol[i], i + 1);
scanf("%d", &mainPol[k][1]);
totalTermsInEachPol[i]--;
k++;
}
}
// sorting mainPol array
for (int i = 0; i < totalTerms - 1; i++)
{
for (int j = 0; j < totalTerms - i - 1; j++)
{
if (mainPol[j][1] < mainPol[j + 1][1])
{
int temp = mainPol[j][1];
mainPol[j][1] = mainPol[j + 1][1];
mainPol[j + 1][1] = temp;
temp = mainPol[j][0];
mainPol[j][0] = mainPol[j + 1][0];
mainPol[j + 1][0] = temp;
}
}
}
printf("\n");
for (int i = 0; i < totalTerms; i++)
{
for (int j = i + 1; j < totalTerms; j++)
{
if (mainPol[i][1] == mainPol[j][1])
{
mainPol[i][0] += mainPol[j][0];
mainPol[j][0] = 0;
mainPol[j][1] = 0;
}
}
}
for (int i = 0; i < totalTerms - 1; i++)
{
for (int j = 0; j < totalTerms - i - 1; j++)
{
if (mainPol[j][1] < mainPol[j + 1][1])
{
int temp = mainPol[j][1];
mainPol[j][1] = mainPol[j + 1][1];
mainPol[j + 1][1] = temp;
temp = mainPol[j][0];
mainPol[j][0] = mainPol[j + 1][0];
mainPol[j + 1][0] = temp;
}
}
}
printf("\n");
while (mainPol[totalTerms - 1][0] == 0)
{
totalTerms--;
}
printf("The sum of the polynomial equations is: ");
for (int i = 0; i < totalTerms; i++)
{
if (mainPol[i][0] != 0)
{
printf("%dX^%d", mainPol[i][0], mainPol[i][1]);
if (i != totalTerms - 1)
{
printf(" + ");
}
}
}
}