Problem Set 3: Plurality
Evidence of a 5-point Submission
vote
function iterates over candidates array usingcandidate_count
and usesstrcmp
to compare the candidate name to the input name; uses the++
operator and short-circuits when a match is foundprint_winner
function iterates over candidates array at most twiceprint_winner
function may initialize the maximum vote count to the number of votes ofcandidates[0]
, but then starts the for loop at an index of 1 to avoid a redundant first comparison
Evidence of a 4-point Submission
vote
function iterates over candidates array usingcandidate_count
and usesstrcmp
to compare the candidate name to the input name; uses the++
operator and short-circuits when a match is foundprint_winner
function iterates over candidates array at most twiceprint_winner
function may initialize the maximum vote count to the number of votes ofcandidates[0]
, but then starts the for loop at an index of 0 (first comparison will then become redundant)
Evidence of a 3-point Submission
vote
function iterates over candidates array usingcandidate_count
and usesstrcmp
to compare the candidate name to the input name; short-circuits when a match is foundprint_winner
function iterates over the candidates array at most twiceprint_winner
function may use a fast sort to order the array of candidates by number of votes, orprint_winner
function may use twofor
loops but unnecessarily check if a candidate’s vote count is >= the current max count
Evidence of a 2-point Submission
vote
function does not short-circuit, uses unnecessary variables, returns1
instead offalse
or0
instead oftrue
(not explicitly boolean)print_winner
function uses a slow sort to order the array of candidates by number of votes, or uses afor
loop that compares thei
th candidate with thei+1
th candidate (extraneous comparisons). May also declare more than one variable or store the winners in an array.
Evidence of a 1-point Submission
vote
function does not use thestrcmp
function (compares the two strings by looping over each character to compare one to the other), or uses another method that is confusingprint_winner
function uses a method that is confusing or unnecessary, may include a method of checking for ties that does not always work (prints out winners before the entire array of votes has been iterated through)print_winner
function may initialize the maximum vote counter to a number that does not make sense (for example: 1)
Example Implementations (Worse vs. Better)
vote
Function
Worse Implementation
The example below will not short-circuit when a match is found.
int num_matches = 0;
for (int i = 0; i < candidate_count; i++)
{
if (!strcmp(name, candidates[i].name))
{
candidates[i].votes++;
num_matches++;
}
}
if (num_matches > 0)
{
return true;
}
else
{
return false;
}
Better Implementation
The example below will short-circuit, and uses no extraneous variables
for (int i = 0; i < candidate_count; i++)
{
if (!strcmp(name, candidates[i].name))
{
candidates[i].votes++;
return true;
}
}
return false;
print_winner
Function
Worse Implementation
The example below introduces an extraneous variable that leads to the need for a repeated print statement
int top = candidates[0].votes;
int top_index = 0;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > top)
{
top = candidates[i].votes;
top_index = i;
}
}
printf("%s\n", candidates[top_index].name);
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == top && top_index != i)
{
printf("%s\n", candidates[i].name);
}
}
Though the example below is very similar to the better implementation and is still functionally correct, it unnecessarily uses the >=
operator in both if
statements
int max = 0;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes >= max)
{
max = candidates[i].votes;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes >= max)
{
printf("%s\n", candidates[i].name);
}
}
Better Implementation
The example below uses no extraneous variables, comparisons, or for
loops
int max = 0;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > max)
{
max = candidates[i].votes;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == max)
{
printf("%s\n", candidates[i].name);
}
}