plurality.c Code

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
const int 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)) // checking and reciving false
        {
            // printf("candidates votes of %s is %i\n", name, candidates[i].votes);
            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++)
    {
        // printf("in the loop of the vote function!\ngoing to check: %s = %s\n",
        // candidates[i].name, name);
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes += 1;
            // printf("current value of %s is %i\n", candidates[i].name, candidates[i].votes);
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int most_votes = candidates[0].votes;     // test[0], bob[1], alice[2] = test[0]
    for (int i = 0; i < candidate_count; i++) // output candidate with the biggest number
    {
        if (candidates[i].votes > most_votes) //[0]=2>[0]=2; [1]=1>[0]=2; [2]=0>[0]=2
        {
            most_votes = candidates[i].votes; // test[0] = 2
        }
    }

    for (int i = 0; i < candidate_count; i++)
    {
        // printf("candidates score: %s(%i)\n",candidates[i].name,candidates[i].votes);
        if (candidates[i].votes == most_votes)
        {
            printf("%s\n", candidates[i].name);
        }
    }
}

runoff.c Code

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max voters and candidates
const int MAX_VOTERS = 100;
const int 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 %i\n", 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 %i\n", 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("%s\n", 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)
{
    for (int number = 0; number < candidate_count; number++)
    {
        if (strcmp(candidates[number].name, name) == 0)
        {
            preferences[voter][rank] = number;
            return true;
        }
    }
    return false;
}

// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    // TODO
    int j = 1;
    for (int voter = 0; voter < voter_count; voter++)
    {
        if (candidates[preferences[voter][0]].eliminated != 1)
        {
            candidates[preferences[voter][0]].votes += 1;
        }
        else
        {
            while (candidates[preferences[voter][j]].eliminated)
            {
                j++;
            }
            preferences[voter][0] = preferences[voter][j];
            candidates[preferences[voter][0]].votes += 1;
        }
    }
}

/* else
 {
     for (int voter = 0; voter < voter_count; voter++)
     {
         if (preferences[voter][0] == candidate)
         {
             preferences[voter][0] = preferences[voter][1];
         }
     }
 }*/

// going through each voter

/*for (int voter = 0; voter < voter_count; voter++)
{
    printf("voter: %i picked %i = candidates[%i] ?\n", voter, preferences[voter][0], candidate);
    if (preferences[voter][0] == candidate)
    {
        printf("yes picked %i = candidates[%i]\n so: candidates[%i].votes (%i) += 1\n",
preferences[voter][0], candidate, candidate,candidates[candidate].votes);
        candidates[candidate].votes += 1;
    }
}*/

bool print_winner(void)
{
    // double divide=0;
    for (int number = 0; number < candidate_count; number++)
    {
        // divide=(candidates[number].votes/(float)voter_count);
        //  printf("dividing candidates(%i)votes which are: %i / %i = %f is this > 0.50 ?\n",
        //  number, candidates[number].votes ,voter_count, divide);
        if ((candidates[number].votes / (float) voter_count) > 0.50)
        {
            printf("%s\n", candidates[number].name);
            return true;
        }
    }
    return false;
}
/*each voter has a prefrence = 0, 1 , 2... (candidates)
how to calculate if the majority voted > 50% as the first prefrence*/

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    // TODO
    int min = 1410065407; //max registered number of an int
    // printf("saved min as %i[0 - from candidate' votes]\n", candidates[0].votes);
    for (int number = 0; number < candidate_count; number++)
    {
        // printf("cmp: candidates[%i].votes (%i - candidates votes) <
        // min(%i)\n",number,candidates[number].votes,min);
        if (candidates[number].eliminated != 1)
        {
            if (candidates[number].votes < min)
            {
                //  printf("--------------------------------------------------------\n");
                //   printf("new minimum of candidate.votes got saved!: candidates[%i].votes (%i -
                //   candidates votes) in variable min, which was %i\n",
                //   number,candidates[number].votes, min);
                //  printf("--------------------------------------------------------\n");
                min = candidates[number].votes;
            }
        }
    }
    return min;
}
// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min) // min is the least votes a specific candidate got
{
    // TODO
    int tie_counter = 0;
    int eliminated_counter = 0
    for (int i = 0; i < candidate_count; i++)
    {
        if ((candidates[i].eliminated != 1) && candidates[i].votes == min)
        {
            tie_counter++;
        }
        else if (candidates[i].eliminated == 1)
        {
            eliminated_counter++;
        }
    }
    if (tie_counter == (candidate_count - eliminated_counter))
    {
        return true;
    }
    return false;
}

// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
    for (int number = 0; number < candidate_count; number++)
    {
        if (candidates[number].votes == min)
        {
            candidates[number].eliminated = 1;
        }
    }
}