Complete the tabulate function.
The function should update the number of votes each candidate has at this stage in the runoff.
Recall that at each stage in the runoff, every voter effectively votes for their top-preferred candidate who has not already been eliminated.
As you write your code, consider these hints:
Recall that voter_count stores the number of voters in the election and that, for each voter in our election, we want to count one ballot.
Recall that for a voter i, their top choice candidate is represented by preferences[i][0], their second choice candidate by preferences[i][1], etc.
Recall that the candidate struct has a field called eliminated, which will be true if the candidate has been eliminated from the election.
Recall that the candidate struct has a field called votes, which you’ll likely want to update for each voter’s preferred candidate.
Recall that once you’ve cast a vote for a voter’s first non-eliminated candidate, you’ll want to stop there, not continue down their ballot. You can break out of a loop early using break inside of a conditional.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];
// Candidates have name, vote count, eliminated status
typedef struct
{
string name;
int votes;
bool eliminated;
} candidate;
// Array of candidates
candidate candidates[MAX_CANDIDATES];
// Numbers of voters and candidates
int voter_count;
int candidate_count;
// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: runoff [candidate ...]n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX_CANDIDATES)
{
printf("Maximum number of candidates is %in", MAX_CANDIDATES);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
candidates[i].eliminated = false;
}
voter_count = get_int("Number of voters: ");
if (voter_count > MAX_VOTERS)
{
printf("Maximum number of voters is %in", MAX_VOTERS);
return 3;
}
// Keep querying for votes
for (int i = 0; i < voter_count; i++)
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
// Record vote, unless it's invalid
if (!vote(i, j, name))
{
printf("Invalid vote.n");
return 4;
}
}
printf("n");
}
// Keep holding runoffs until winner exists
while (true)
{
// Calculate votes given remaining candidates
tabulate();
// Check if election has been won
bool won = print_winner();
if (won)
{
break;
}
// Eliminate last-place candidates
int min = find_min();
bool tie = is_tie(min);
// If tie, everyone wins
if (tie)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
printf("%sn", candidates[i].name);
}
}
break;
}
// Eliminate anyone with minimum number of votes
eliminate(min);
// Reset vote counts back to zero
for (int i = 0; i < candidate_count; i++)
{
candidates[i].votes = 0;
}
}
return 0;
}
// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
preferences[voter][rank] = i;
return true;
}
}
return false;
}
// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
// TODO
for (int column = 0; column < candidate_count; column++)
{
for (int row = 0; row < voter_count; row++)
{
if (!candidates[preferences[row][column]].eliminated)
{
candidates[preferences[row][column]].votes++;
}
else
{
for (int i = column + 1; i < candidate_count; i++)
{
if(!candidates[preferences[row][i]].eliminated)
{
candidates[preferences[row][i]].votes++;
break;
}
}
}
}
}
return;
}
// Print the winner of the election, if there is one
bool print_winner(void)
{
// TODO
int tracker = 0;
int total_votes = 0;
for (int i = 0; i < candidate_count; i++)
{
if(!candidates[i].eliminated)
{
total_votes += candidates[i].votes;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > candidates[tracker].votes && !candidates[i].eliminated)
{
tracker = i;
}
}
if (candidates[tracker].votes > (total_votes)/2.0)
{
printf("winner is %sn",candidates[tracker].name);
return true;
}
return false;
}
// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
// TODO
int tracker = 0;
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated && (candidates[i].votes < candidates[tracker].votes))
{
tracker = i;
}
else if (!candidates[i].eliminated && (candidates[i].votes == candidates[tracker].votes) && i != tracker)
{
return candidates[i].votes;
}
}
return candidates[tracker].votes;
}
// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (min != candidates[i].votes && !candidates[i].eliminated)
{
return false;
}
}
return true;
}
// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated && min == candidates[i].votes)
{
candidates[i].eliminated = true;
}
}
return;
}
and these are the errors
🙁 tabulate counts votes when all candidates remain in election
Cause
tabulate function did not produce correct vote totals
🙁 tabulate counts votes when one candidate is eliminated
Cause
tabulate function did not produce correct vote totals
🙁 tabulate counts votes when multiple candidates are eliminated
Cause
tabulate function did not produce correct vote totals
🙁 tabulate handles multiple rounds of preferences
Cause
tabulate function did not produce correct vote totals
i know you will suggest me to interchange row and column iteration but i dont want to do i think my code is correct as the in outer loop column remains constant and in inner loop row changes as i expect to to count each voters first preference which my code is doing and when the candidate is eliminated and for that i have made another loop where column changes but row same and count vote and break
if you find any logical error plz help me out with a explanation
user25937449 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.