-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseat_alloc_utils.c
54 lines (49 loc) · 1018 Bytes
/
seat_alloc_utils.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
#pragma once
#include "seat_alloc_utils.h"
party party_new(const party_id_type id, int votes)
{
party p;
p.id = id;
p.votes = votes;
p.seats = 0;
return p;
}
int party_get_seats(const int partyc, party *parties, const party_id_type id)
{
for (int i = 0; i < partyc; i++)
{
party p = parties[i];
if (p.id == id)
{
return p.seats;
}
}
return -1;
}
int total_valid_poll(const int partyc, party *parties)
{
int tvp = 0;
for (int i = 0; i < partyc; i++)
{
tvp += parties[i].votes;
}
return tvp;
}
int threshold(const int partyc, party *parties, double pct)
{
int tv = total_valid_poll(partyc, parties);
int th = floor(tv * pct);
party filtered[partyc];
int new_size = 0;
for (int i = 0; i < partyc; i++)
{
party p = parties[i];
if (p.votes >= th)
{
*filtered = p;
new_size++;
}
}
parties = filtered;
return new_size;
}