-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationalNumberCollection.cpp
More file actions
412 lines (347 loc) · 11.7 KB
/
RationalNumberCollection.cpp
File metadata and controls
412 lines (347 loc) · 11.7 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "RationalNumberCollection.h"
#include "stdio.h"
#include "stdlib.h"
/*This struct represents a single RationalNumber in the RationalNumberCollection. In addition to the
numerator and denominator, it contains an int for the count.*/
struct RationalNumberCollectionElement{
RationalNumber rn;
int count;
};
/*A RationalNumberCollection can contain different RationalNumbers
and contains the count of each kind of RationalNumber*/
struct RationalNumberCollection {
RationalNumberCollectionElement* collection;
// totalUniqueCount can also be used as currentLength
int totalUniqueCount;
int totalCount;
int capacity;
RationalNumber sum;
RationalNumber average;
RationalNumber median;
};
bool rncInit(RationalNumberCollection* c, int capacity)
{
c->average.numerator = 0;
c->average.denominator = 0;
c->median.numerator = 0;
c->median.denominator = 0;
c->sum.numerator = 0;
c->sum.denominator = 0;
c->totalCount = 0;
c->totalUniqueCount = 0;
c->capacity = capacity;
for (int i = 0; i<capacity; i++)
{
c->collection[i].rn.numerator = 0;
c->collection[i].rn.denominator = 0;
c->collection[i].count = 0;
}
return true;
}
RationalNumberCollection *rncCreate(int capacity)
{
RationalNumberCollection *c = (RationalNumberCollection*) malloc(sizeof(RationalNumberCollection));
c->collection = (RationalNumberCollectionElement*) malloc(capacity * sizeof(RationalNumberCollectionElement));
rncInit(c, capacity);
return c;
}
void rncDelete(RationalNumberCollection *c)
{
free(c->collection);
free(c);
}
// This function is called, when the capacity of the collection is not sufficient. After this function the capacity is 50% larger, and at least 10.
void rncIncreaseCapacity(RationalNumberCollection *c)
{
int newCapicity;
newCapicity = c->capacity + (c->capacity / 2);
if (newCapicity < 10) newCapicity = 10;
RationalNumberCollectionElement *newCollection = (RationalNumberCollectionElement *)malloc(newCapicity*sizeof(RationalNumberCollectionElement));
// copying the old values into the newCollection
for (int i=0; i<c->capacity; i++)
{
newCollection[i] = c->collection[i];
}
// free the old collection and set the collection pointer to the new one.
free(c->collection);
c->collection = newCollection;
c->capacity = newCapicity;
}
// This function returns the position of the given RationalNumber in the collection, if the collection contains the given RationalNumber.
// It returns -1 if it doesn't contain it.
int rncGetPosition(RationalNumberCollection *c, RationalNumber n)
{
for (int i = 0; i<c->totalUniqueCount; i++)
{
if (c->collection[i].rn.numerator == n.numerator)
{
if (c->collection[i].rn.denominator == n.denominator)
{
return i;
}
}
}
return -1;
}
// this function returns true, if the capacity is fully stretched
bool rncIsFull(RationalNumberCollection *c)
{
if (c->totalUniqueCount >= c->capacity)
{
return true;
}
return false;
}
void rncUpdateTotalCount(RationalNumberCollection *c)
{
int result = 0;
for (int i=0 ; i < c->totalUniqueCount ; i++)
{
result += c->collection[i].count;
}
c->totalCount = result;
}
// This function calculates the sum of the collection by adding all of the RationalNumbers
void rncUpdateSum(RationalNumberCollection *c)
{
// resultSum is 1/1 at first (so that it is not invalid)
// this is subtracted before resultSum is returned
RationalNumber resultSum;
resultSum.numerator = 1;
resultSum.denominator = 1;
for (int i=0; i < c->totalUniqueCount; i++)
{
for (int j=0; j<c->collection[i].count; j++)
{
resultSum = rnAdd(resultSum, c->collection[i].rn);
}
}
// Subtracting 1/1 from resultSum
RationalNumber resultSumDiff;
resultSumDiff.numerator = 1;
resultSumDiff.denominator = 1;
resultSum = rnSubtract(resultSum, resultSumDiff);
c->sum = resultSum;
// In case of the collection is empty, the sum is 0/0
if (c->totalUniqueCount == 0)
{
c->sum.numerator = 0;
c->sum.denominator = 0;
}
}
// This function calculates the average value of the collection by dividing the sum by the number of RationalNumbers
void rncUpdateAverage(RationalNumberCollection *c)
{
if (c->totalCount < 1)
{
c->average.numerator = 0;
c->average.denominator = 0;
}
else
{
RationalNumber totalCountRN;
totalCountRN.numerator = c->totalCount;
totalCountRN.denominator = 1;
RationalNumber result = rnDivide(c->sum, totalCountRN);
c->average = result;
}
}
// This function gets the current median value by calculating which rational number has the smallest difference to the average
void rncUpdateMedian(RationalNumberCollection *c)
{
if (c->totalCount < 1)
{
c->median.numerator = 0;
c->median.denominator = 0;
}
else
{
RationalNumber averageStart = c->average;
RationalNumber resultRNDiff;
int resultRNPosition;
RationalNumber averageLoop;
RationalNumber currentPositionRNDiff;
for (int i=0; i<c->totalUniqueCount; i++)
{
averageLoop = c->average;
RationalNumber currentPositionRN = c->collection[i].rn;
if (rnIsNegative(averageLoop) && rnIsNegative(currentPositionRN))
{
averageLoop.numerator = abs(averageLoop.numerator);
averageLoop.denominator = abs(averageLoop.denominator);
currentPositionRN.numerator = abs(currentPositionRN.numerator);
currentPositionRN.denominator = abs(currentPositionRN.denominator);
if (rnLessThan(averageLoop,currentPositionRN))
{
currentPositionRNDiff = rnSubtract(currentPositionRN, averageLoop);
}
else
{
currentPositionRNDiff = rnSubtract(averageLoop, currentPositionRN);
}
}
else if (rnIsNegative(averageLoop))
{
averageLoop.numerator = abs(averageLoop.numerator);
averageLoop.denominator = abs(averageLoop.denominator);
currentPositionRNDiff = rnAdd(averageLoop, currentPositionRN);
}
else if (rnIsNegative(currentPositionRN))
{
currentPositionRN.numerator = abs(currentPositionRN.numerator);
currentPositionRN.denominator = abs(currentPositionRN.denominator);
currentPositionRNDiff = rnAdd(averageStart, currentPositionRN);
}
else
{
if (rnLessThan(averageLoop,currentPositionRN))
{
currentPositionRNDiff = rnSubtract(currentPositionRN, averageLoop);
}
else
{
currentPositionRNDiff = rnSubtract(averageLoop, currentPositionRN);
}
}
if (i==0)
{
resultRNDiff = currentPositionRNDiff;
resultRNPosition = i;
}
// saving the position of the value most near to the average
if (rnLessThan(currentPositionRNDiff, resultRNDiff))
{
resultRNDiff = currentPositionRNDiff;
resultRNPosition = i;
}
}
c->median = c->collection[resultRNPosition].rn;
}
}
// This method is called to update all informational elements of the collection after an element has been added or removed
void rncUpdateCollection(RationalNumberCollection *c)
{
rncUpdateTotalCount(c);
rncUpdateSum(c);
rncUpdateAverage(c);
rncUpdateMedian(c);
}
int rncCount(RationalNumberCollection *c, RationalNumber n)
{
int position;
position = rncGetPosition(c, n);
if (position != -1) return c->collection[position].count;
return 0;
}
bool rncAdd(RationalNumberCollection *c, RationalNumber n)
{
int position = rncGetPosition(c, n);
if (position != -1)
{
c->collection[position].count++;
rncUpdateCollection(c);
return true;
}
// if the capacity is insufficient rncIncreaseCapacity is called and rncAdd is called again.
else if (rncIsFull(c))
{
rncIncreaseCapacity(c);
return rncAdd(c, n);
}
// When a new RationalNumber is added to an empty collection, the order is ignored.
else if (c->totalUniqueCount == 0)
{
c->collection[c->totalUniqueCount].rn.numerator = n.numerator;
c->collection[c->totalUniqueCount].rn.denominator = n.denominator;
c->collection[c->totalUniqueCount].count = 1;
// increasing the current length of the collection (via totalUniqueCount)
c->totalUniqueCount++;
rncUpdateCollection(c);
return true;
}
// When a new RationalNumber is added to a filled collection, it checks for the right position (the collection is ordered in ascending values)
else
{
// this int represents the position in which the new value is saved for providing the ascending order.
// if the rational number is larger than all of the other entries, it gets to the last position.
int insertPosition = c->totalUniqueCount;
for (int i=0; i<c->totalUniqueCount; i++)
{
if (rnLessThan(n, c->collection[i].rn))
{
insertPosition = i;
break;
}
}
// moving all following RationalNumbers to keep the ascending order
for (int i=c->totalUniqueCount; i>insertPosition; i--)
{
c->collection[i].rn = c->collection[i-1].rn;
c->collection[i].count = c->collection[i-1].count;
}
// addind the new RationalNumber in the correct position
c->collection[insertPosition].rn.numerator = n.numerator;
c->collection[insertPosition].rn.denominator = n.denominator;
c->collection[insertPosition].count = 1;
// increasing the currentLength of the collection
c->totalUniqueCount++;
rncUpdateCollection(c);
return true;
}
}
bool rncRemove(RationalNumberCollection *c, RationalNumber n)
{
int position;
position = rncGetPosition(c, n);
// case one: the RationalNumber is not in the collection
if (position == -1)
{
return true;
}
// case two: the RationalNumber is in the collection more than one time
else if (c->collection[position].count > 1)
{
c->collection[position].count--;
rncUpdateCollection(c);
return false;
}
// case three: the RationalNumber is in the collection one time. The Following entries in the collection
// have to be moved to avoid an empty index
else
{
for (int i=position; i<c->totalUniqueCount; i++)
{
RationalNumber tempRN = c->collection[i+1].rn;
int tempCount = c->collection[i+1].count;
c->collection[i].rn = tempRN;
c->collection[i].count = tempCount;
}
c->totalUniqueCount--;
rncUpdateCollection(c);
return true;
}
}
int rncTotalCount(RationalNumberCollection *c)
{
return c->totalCount;
}
int rncTotalUniqueCount(RationalNumberCollection *c)
{
return c->totalUniqueCount;
}
RationalNumber rncSum(RationalNumberCollection *c)
{
return c->sum;
}
RationalNumber rncAverage(RationalNumberCollection *c)
{
return c->average;
}
RationalNumber rncMedian(RationalNumberCollection *c)
{
return c->median;
}
RationalNumber rncGetRNAtPosition(RationalNumberCollection *c,int i)
{
return c->collection[i].rn;
}