Problem Set 3: Plurality

Evidence of a 5-point Submission

  • vote function iterates over candidates array using candidate_count and uses strcmp to compare the candidate name to the input name; uses the ++ operator and short-circuits when a match is found
  • print_winner function iterates over candidates array at most twice
  • print_winner function may initialize the maximum vote count to the number of votes of candidates[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 using candidate_count and uses strcmp to compare the candidate name to the input name; uses the ++ operator and short-circuits when a match is found
  • print_winner function iterates over candidates array at most twice
  • print_winner function may initialize the maximum vote count to the number of votes of candidates[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 using candidate_count and uses strcmp to compare the candidate name to the input name; short-circuits when a match is found
  • print_winner function iterates over the candidates array at most twice
  • print_winner function may use a fast sort to order the array of candidates by number of votes, or print_winner function may use two for 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, returns 1 instead of false or 0 instead of true (not explicitly boolean)
  • print_winner function uses a slow sort to order the array of candidates by number of votes, or uses a for loop that compares the ith candidate with the i+1th 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 the strcmp function (compares the two strings by looping over each character to compare one to the other), or uses another method that is confusing
  • print_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;

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);
    }
}