summaryrefslogtreecommitdiff
path: root/wk3/pset/runoff
diff options
context:
space:
mode:
authorFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-02-19 20:32:31 +0000
committerFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-02-19 20:32:31 +0000
commit6bec04c8ff49f14b96e1a37fc2ab2fb89af04064 (patch)
tree47a08494fc796cc37f685819ed0a8ce3f4c22d36 /wk3/pset/runoff
parentebfa5391ffceb15dfbf2e4ad8741e4893e160423 (diff)
Mon, Feb 19, 2024, 12:32 PM -08:00
Diffstat (limited to 'wk3/pset/runoff')
-rw-r--r--wk3/pset/runoff/runoff.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/wk3/pset/runoff/runoff.c b/wk3/pset/runoff/runoff.c
index c9ede93..4daa519 100644
--- a/wk3/pset/runoff/runoff.c
+++ b/wk3/pset/runoff/runoff.c
@@ -164,7 +164,38 @@ void tabulate(void)
// Print the winner of the election, if there is one
bool print_winner(void)
{
- // TODO
+ int winners[candidate_count];
+ winners[0] = 0;
+ // reset it
+ for (int j = 1; j < candidate_count; j++)
+ {
+ winners[j] = -1;
+ }
+ //
+ for (int i = 1; i < candidate_count; i++)
+ {
+ if (candidates[winners[0]].votes < candidates[i].votes) // if the new candidate has more votes than previous
+ {
+ // clear the list of winners
+ for (int j = 1; j < candidate_count; j++)
+ {
+ winners[j] = -1;
+ }
+ // put it at the start of the new list
+ winners[0] = i;
+ }
+ else if (candidates[winners[0]].votes == candidates[i].votes)
+ {
+ for (int j = 1; j < candidate_count; j++)
+ {
+ if (winners[j] == -1)
+ {
+ winners[j] = j;
+ break;
+ }
+ }
+ }
+ }
return false;
}