Implement a program that runs a Tideman election, per the below.
./tideman Alice Bob Charlie
Number of voters: 5
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Charlie
Rank 2: Alice
Rank 3: Bob
Charlie
Complete the implementation of tideman.c
in such a way that it simulates a Tideman election.
- Complete the
vote
function. The function takes argumentsrank
,name
, andranks
. Ifname
is a match for the name of a valid candidate, then you should update theranks
array to indicate that the voter has the candidate as theirrank
preference (where0
is the first preference,1
is the second preference, etc.) Recall thatranks[i]
here represents the user’s ith preference. The function should returntrue
if the rank was successfully recorded, andfalse
otherwise (if, for instance,name
is not the name of one of the candidates). You may assume that no two candidates will have the same name. - Complete the
record_preferences
function. The function is called once for each voter, and takes as argument theranks
array, (recall thatranks[i]
is the voter’si
th preference, whereranks[0]
is the first preference). The function should update the globalpreferences
array to add the current voter’s preferences. Recall thatpreferences[i][j]
should represent the number of voters who prefer candidatei
over candidatej
. You may assume that every voter will rank each of the candidates. - Complete the
add_pairs
function. The function should add all pairs of candidates where one candidate is preferred to thepairs
array. A pair of candidates who are tied (one is not preferred over the other) should not be added to the array. The function should update the global variablepair_count
to be the number of pairs of candidates. (The pairs should thus all be stored betweenpairs[0]
andpairs[pair_count - 1]
, inclusive). - Complete the
sort_pairs
function. The function should sort thepairs
array in decreasing order of strength of victory, where strength of victory is defined to be the number of voters who prefer the preferred candidate. If multiple pairs have the same strength of victory, you may assume that the order does not matter. - Complete the
lock_pairs
function. The function should create thelocked
graph, adding all edges in decreasing order of victory strength so long as the edge would not create a cycle. - Complete the
print_winner
function. The function should print out the name of the candidate who is the source of the graph. You may assume there will not be more than one source. You should not modify anything else intideman.c
other than the implementations of thevote
,record_preferences
,add_pairs
,sort_pairs
,lock_pairs
, andprint_winner
functions (and the inclusion of additional header files, if you’d like). You are permitted to add additional functions totideman.c
, so long as you do not change the declarations of any of the existing functions.
Your program should behave per the example below:
./tideman Alice Bob Charlie
Number of voters: 5
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Charlie
Rank 2: Alice
Rank 3: Bob
Charlie
Full instructions available here