summaryrefslogtreecommitdiff
path: root/wk3
diff options
context:
space:
mode:
authorFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-02-19 04:55:29 +0000
committerFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-02-19 04:55:29 +0000
commitaee1750577d4ed487bd0037905debfdb91828817 (patch)
treea03f03d5bacfc90cafbc4d95dab837f2b10899db /wk3
parent4f5ecbbcfb5658389e2de321f5c8a16ce26f1ec3 (diff)
Sun, Feb 18, 2024, 8:55 PM -08:00
Diffstat (limited to 'wk3')
-rw-r--r--wk3/pset/plurality/plurality.c16
1 files changed, 7 insertions, 9 deletions
diff --git a/wk3/pset/plurality/plurality.c b/wk3/pset/plurality/plurality.c
index 48aedd6..8cfbf51 100644
--- a/wk3/pset/plurality/plurality.c
+++ b/wk3/pset/plurality/plurality.c
@@ -50,7 +50,6 @@ int main(int argc, string argv[])
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
- printf("vote is for %s\n", name);
// Check for invalid vote
if (!vote(name))
@@ -66,10 +65,9 @@ int main(int argc, string argv[])
// Update vote totals given a new vote
bool vote(string name)
{
- for (int i = 0; i < candidate_count - 1; i++)
+ for (int i = 0; i < candidate_count; i++)
{
- printf("candidate is %s\n", candidates[i].name);
- if (name == candidates[i].name)
+ if (!strcmp(name, candidates[i].name))
{
candidates[i].votes++;
return true;
@@ -81,14 +79,14 @@ bool vote(string name)
// Print the winner (or winners) of the election
void print_winner(void)
{
- string winners[candidate_count - 1];
+ string winners[candidate_count];
winners[0] = candidates[0].name;
- for (int i = 1; i < candidate_count - 1; i++)
+ for (int i = 1; i < candidate_count; i++)
{
if (winners[0] < candidates[i].name) // if the new candidate has more votes than all previous
{
// clear the list of winners
- for (int j = 1; j < candidate_count - 1; j++)
+ for (int j = 1; j < candidate_count; j++)
{
winners[j] = 0;
}
@@ -97,7 +95,7 @@ void print_winner(void)
}
else if (winners[0] == candidates[i].name)
{
- for (int j = 1; j < candidate_count - 1; j++)
+ for (int j = 1; j < candidate_count; j++)
{
if (winners[j] == 0)
{
@@ -106,7 +104,7 @@ void print_winner(void)
}
}
}
- for (int i = 1; i < candidate_count - 1; i++)
+ for (int i = 1; i < candidate_count; i++)
{
if (winners[i] != 0)
{