-
Notifications
You must be signed in to change notification settings - Fork 1
/
plurality.c
123 lines (110 loc) · 2.82 KB
/
plurality.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <cs50.h>
#include <strings.h>
#include <stdio.h>
#include <ctype.h>
// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedef struct
{
string name;
int votes;
}
candidate;
// Array of candidates
candidate candidates[MAX];
// Number of candidates
int candidate_count;
// Function prototypes
bool vote(string name);
void print_winner(void);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count = get_int("Number of voters: ");
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
for (int i = 0; i < candidate_count; i++)
{
// Compare the name typed by the user with the candidates names, ignoring case
if (strcasecmp(candidates[i].name, name) == 0)
{
candidates[i].votes ++;
return true;
}
}
// If any candidate was found, so the candidate name typed by the user is invalid
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
int higher = 0;
// Let's compare the number of votes of which one
for (int i = 0; i < candidate_count - 1; i++)
{
if (i == 0)
{
// If they are equal, doesn't matter if its the index of 'i' or 'i + 1'
if (candidates[i].votes >= candidates[i + 1].votes)
{
higher = candidates[i].votes;
}
if (candidates[i + 1].votes > candidates[i].votes)
{
higher = candidates[i + 1].votes;
}
}
else
{
// If a candidate's votes number is higher than the 'higher' variable, the 'higher' receives that value
if (candidates[i].votes > higher)
{
higher = candidates[i].votes;
}
if (candidates[i + 1].votes > higher)
{
higher = candidates[i + 1].votes;
}
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == higher)
{
printf("%s\n", candidates[i].name);
}
}
return;
}