The Australian Electoral Commission (AEC) Online Voting System requires the following computations:
- Pre-populate its system with registered voters from an existing data source
- Allow users to sign-up using an unique email address
- Let voters rank a pre-determined list of candidates
- Calculate live tallies of the results so far
- Determine the overall winner of an election
Algorithm: step-by-step procedures required to solve a problem.
Pseudocode: a type of descriptive algorithm that is a mixture of everyday language and programming languages.
BEGIN launch(prereg_voters) IF election_event_name UNDEFINED THEN INPUT GLOBAL election_event_name ENDIF IF election_event_candidates UNDEFINED THEN SET GLOBAL election_event_candidates TO {} WHILE INPUT candidate_name SET candidate { name: candidate_name, result: { rank: 0 } } APPEND candidate TO election_event_candidates ENDWHILE ENDIF IF voter_list UNDEFINED THEN SET GLOBAL voter_list TO [] ENDIF #populate existing voter list into application data structure: IF prereg_voters DEFINED THEN FOR each_voter IN prereg_voters: SET voter { email: prereg_voters[email], pword: prereg_voters[password], election: [{ event: election_event_name, votes: [] }], } APPEND voter TO voter_list ENDFOR ENDIF END launch()
BEGIN register() INPUT new_email INPUT new_password SET existing_voter TO false FOR registered_voter IN voter_list: IF registered_voter[email] == new_email THEN SET existing_voter TO true ENDIF ENDFOR IF NOT existing_voter THEN SET voter { email: new_email, pword: new_password, election: [{ event: election_event_name, votes: [] }], } APPEND voter TO voter_list ENDIF END register()
BEGIN authenticate() INPUT login_attempt_email INPUT login_attempt_password SET authenticated_voter TO NULL FOR registered_voter IN voter_list: IF registered_voter[email] == login_attempt_email THEN IF registered_voter[pword] == login_attempt_password THEN SET authenticated_voter TO registered_voter ENDIF ENDIF ENDFOR RETURN authenticated_voter END authenticate()
BEGIN voting() INPUT close_voting_timedate SET now_timedate TO SYSTEM.TIME WHILE now_timedate < close_voting_timedate voter = authenticate() IF voter IS NOT NULL THEN FOR candidate IN election_event_candidates: INPUT preference_ranking SET voter[election][votes][preference_ranking] TO candidate[name] ENDFOR ENDIF SET now_timedate TO SYSTEM.TIME END WHILE END voting()
BEGIN tally_votes() FOR vote IN voter_list: FOR preference IN RANGE(LENGTH OF election_event_candidates): SET chosen_candidate TO vote[election][votes][preference] INCREASE election_event_candidates[chosen_candidate][result][rank] BY preference ENDFOR ENDFOR END tally_votes()
BEGIN determine_winner() #set first candidate as default leader on initial run: SET leading_candidate TO election_event_candidates[0][name] SET leading_rank TO election_event_candidates[0][result][rank] FOR candidate IN election_event_candidates: IF candidate[result][rank] > leading_rank THEN SET leading_candidate TO candidate[name] ENDIF ENDFOR RETURN leading_candidate END determine_winner()