{"id":54,"date":"2022-12-13T12:01:14","date_gmt":"2022-12-13T12:01:14","guid":{"rendered":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/?p=54"},"modified":"2022-12-13T12:01:14","modified_gmt":"2022-12-13T12:01:14","slug":"54","status":"publish","type":"post","link":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/?p=54","title":{"rendered":""},"content":{"rendered":"<div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\r\n\t\t\t<div class=\"control-language\">\r\n                <div class=\"dm-buttons\">\r\n                    <div class=\"dm-buttons-left\">\r\n                        <div class=\"dm-button-snippet red-button\"><\/div>\r\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\r\n                        <div class=\"dm-button-snippet green-button\"><\/div>\r\n                    <\/div>\r\n                    <div class=\"dm-buttons-right\">\r\n                        <a id=\"dm-copy-raw-code\">\r\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\r\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\r\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\r\n                    <\/div>\r\n                <\/div>\r\n                <pre class=\"line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-clike\"><\/p>\n<pre class=\"dm-pre-admin-side\">#include &lt;cs50.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n\/\/ Max voters and candidates\r\n#define MAX_VOTERS 100\r\n#define MAX_CANDIDATES 9\r\n\r\n\/\/ preferences[j] is jth preference for voter i\r\nint preferences[MAX_VOTERS][MAX_CANDIDATES];\r\n\r\n\/\/ Candidates have name, vote count, eliminated status\r\ntypedef struct\r\n{\r\nstring name;\r\nint votes;\r\nbool eliminated;\r\n}\r\ncandidate;\r\n\r\n\/\/ Array of candidates\r\ncandidate candidates[MAX_CANDIDATES];\r\n\r\n\/\/ Numbers of voters and candidates\r\nint voter_count;\r\nint candidate_count;\r\n\r\n\/\/ Function prototypes\r\nbool vote(int voter, int rank, string name);\r\nvoid tabulate(void);\r\nbool print_winner(void);\r\nint find_min(void);\r\nbool is_tie(int min);\r\nvoid eliminate(int min);\r\n\r\nint main(int argc, string argv[])\r\n{\r\n\/\/ Check for invalid usage\r\nif (argc &lt; 2)\r\n{\r\nprintf(\u201cUsage: runoff [candidate \u2026]\\n\u201d);\r\nreturn 1;\r\n}\r\n\r\n\/\/ Populate array of candidates\r\ncandidate_count = argc \u2013 1;\r\nif (candidate_count &gt; MAX_CANDIDATES)\r\n{\r\nprintf(\u201cMaximum number of candidates is %i\\n\u201d, MAX_CANDIDATES);\r\nreturn 2;\r\n}\r\nfor (int i = 0; i &lt; candidate_count; i++)\r\n{\r\ncandidates[i].name = argv[i + 1];\r\ncandidates[i].votes = 0;\r\ncandidates[i].eliminated = false;\r\n}\r\n\r\nvoter_count = get_int(\u201cNumber of voters: \u201c);\r\nif (voter_count &gt; MAX_VOTERS)\r\n{\r\nprintf(\u201cMaximum number of voters is %i\\n\u201d, MAX_VOTERS);\r\nreturn 3;\r\n}\r\n\r\n\/\/ Keep querying for votes\r\nfor (int i = 0; i &lt; voter_count; i++)\r\n{\r\n\r\n\/\/ Query for each rank\r\nfor (int j = 0; j &lt; candidate_count; j++)\r\n{\r\nstring name = get_string(\u201cRank %i: \u201c, j + 1);\r\n\r\n\/\/ Record vote, unless it\u2019s invalid\r\nif (!vote(i, j, name))\r\n{\r\nprintf(\u201cInvalid vote.\\n\u201d);\r\nreturn 4;\r\n}\r\nprintf(\u201c%i, %i, %s\\n\u201d, i + 1, j + 1, candidates[preferences[i][j]].name);\r\n}\r\nprintf(\u201c\\n\u201d);\r\n}\r\nprintf(\u201ccomplete\u201d);\r\nreturn 0;\r\n}\r\n\/*\r\n\r\n\/\/ Keep holding runoffs until winner exists\r\nwhile (true)\r\n{\r\n\/\/ Calculate votes given remaining candidates\r\ntabulate();\r\n\r\n\/\/ Check if election has been won\r\nbool won = print_winner();\r\nif (won)\r\n{\r\nbreak;\r\n}\r\n\r\n\/\/ Eliminate last-place candidates\r\nint min = find_min();\r\nbool tie = is_tie(min);\r\n\r\n\/\/ If tie, everyone wins\r\nif (tie)\r\n{\r\nfor (int i = 0; i &lt; candidate_count; i++)\r\n{\r\nif (!candidates[i].eliminated)\r\n{\r\nprintf(\u201c%s\\n\u201d, candidates[i].name);\r\n}\r\n}\r\nbreak;\r\n}\r\n\r\n\/\/ Eliminate anyone with minimum number of votes\r\neliminate(min);\r\n\r\n\/\/ Reset vote counts back to zero\r\nfor (int i = 0; i &lt; candidate_count; i++)\r\n{\r\ncandidates[i].votes = 0;\r\n}\r\n}\r\nreturn 0;\r\n}\r\n\r\n*\/\r\n\r\n\/\/ Record preference if vote is valid\r\nbool vote(int voter, int rank, string name)\r\n{\r\nprintf(\u201cvoter no (via local function taking value of voter as argument).: %i, rank selected (via local function taking value of rank as argument) %i\\n\u201d, voter + 1, rank + 1);\r\nfor (int t = 0; t &lt; candidate_count; t++)\r\n{\r\nif(strcmp(name, candidates[t].name) == 0)\r\n{\r\npreferences[voter][rank] = t;\r\nprintf(\u201cpreference of voter no. %i for rank %i is %s\\n\u201d, voter + 1, rank + 1, candidates[t].name);\r\nreturn 4;\r\n}\r\n}\r\nreturn 0;\r\n}\r\n\r\n\/*\r\n\r\n\/\/ Tabulate votes for non-eliminated candidates\r\nvoid tabulate(void)\r\n{\r\n\r\n\/\/ TODO\r\nreturn;\r\n}\r\n\r\n\/\/ Print the winner of the election, if there is one\r\nbool print_winner(void)\r\n{\r\n\/\/ TODO\r\nreturn false;\r\n}\r\n\r\n\/\/ Return the minimum number of votes any remaining candidate has\r\nint find_min(void)\r\n{\r\n\/\/ TODO\r\nreturn 0;\r\n}\r\n\r\n\/\/ Return true if the election is tied between all candidates, false otherwise\r\nbool is_tie(int min)\r\n{\r\n\/\/ TODO\r\nreturn false;\r\n}\r\n\r\n\/\/ Eliminate the candidate (or candidates) in last place\r\nvoid eliminate(int min)\r\n{\r\n\/\/ TODO\r\nreturn;\r\n}\r\n*\/<\/pre>\n<p><\/code><\/pre>\r\n\t\t\t<\/div>\r\n        <\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":1,"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":55,"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/55"}],"wp:attachment":[{"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/apps.devmaverick.com\/code-snippet-staging\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}