0

shouldn't the compiler consider the 'i' and the 'j' from the for loop in the 'main' function to be the voter and rank ints passed to the functions

    #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;
        }
    
        // check for the number candidates within limit
        candidate_count = argc - 1;
        if (candidate_count > MAX_CANDIDATES)
        {
            printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
            return 2;
        }
        // populate array of candidates
        for (int i = 0; i < candidate_count; i++)
        {
            candidates[i].name = argv[i + 1];
            candidates[i].votes = 0;
            candidates[i].eliminated = false;
        }
        // check to make sure the number of voters is within limit
        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;
                }
                else
                {
                    vote(i, j, name);
                }
            }
    
            printf("\n");
        }
    
        // Keep holding runoffs until winner exists
        while (true)
        {
            // Calculate votes given remaining candidates
            for (int i = 0; i < voter_count; i++)
            {
                for (int j = 0; j < candidate_count; j++)
                {
                    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)
    {
        if (strcmp(name, candidates[j].name) != 0)
        {
            return false;
        }
        else if (strcmp(name, candidates[j].name) == 0)
        {
            preferences[i][j] = j;
        return true;
        }
    }
    
    // Tabulate votes for non-eliminated candidates
    void tabulate(void)
    {
        for (int b = 0; b < voter_count; b++)
        {
            for (int d = 0; d < candidate_count; d++)
            {
                if (candidates[d].eliminated == false)
                {
                    if (preferences[i][j] == d)
                    {
                        candidates[d].votes++;
                    }
                }
                else if (candidates[d].eliminated == true)
                {
                    j++;
                }
            }
        }
        return;
    }
    
    // Print the winner of the election, if there is one
    bool print_winner(void)
    {
        int x = candidates[0].votes;
        for (int i = 0; i < candidate_count; i++)
        {
            if (x < candidates[i + 1].votes)
            {
                x = candidates[i + 1].votes;
            }
        }
        for (int i = 0; i < candidate_count; i++)
        {
            if (x == candidates[i].votes)
            {
                printf("%s\n", candidates[i].name);
            }
        }
        return false;
    }
    
    // Return the minimum number of votes any remaining candidate has
    int find_min(void)
    {
        int y = candidates[0].votes;
        for (int i = 0; i < candidate_count; i++)
        {
            if (y > candidates[i + 1].votes)
            {
                y = candidates[i + 1].votes;
            }
            return y;
        }
    }
    
    // Return true if the election is tied between all candidates, false otherwise
    bool is_tie(int min)
    {
        int l = candidates[0].votes;
        for (int i = 0; i < candidate_count; i++)
        {
            if (l < candidates[i + 1].votes)
            {
                l = candidates[i + 1].votes;
            }
        }
        if ( y = l)
        {
            return true;
        }
        else
        {
        return false;
        }
    }
    
    // Eliminate the candidate (or candidates) in last place
    void eliminate(int min)
    {
        for (int i = 0; i < candidate_count; i++)
        {
            if (min == candidates[i].votes)
            {
                candidates[i].eliminated = true;
            }
        }
        return;
    }
0

i and j are not declared in your vote and tabulate functions. Your variables only exist within scope (meaning the { }) in which they are declared. So, if you want to use i and j within these two functions, you will need to declare them; the program will not be able to use a function that is declared within main outside of that function because it is no longer working within it.

Think of this like you would rooms in your house. If you want to use your coffee maker, you need to go to your kitchen because that's where you established that it is. If you want to use your coffee maker in your bedroom, then you either need to move where you keep your coffee maker (change where you declared your variable) or get a coffee maker for this room (declare a variable for this function).

You can use variables of the same name in different scopes, so each function can have a set of variables named i and j. So, just declare your variables in these functions to use them. You could also create what is called a global variable, which would exist in all functions, but a) you do not want this here and b) they are frowned upon and should only be used when absolutely necessary.

Hope this helps clear things up. If so, feel free to click the check mark next to my answer. But if not, leave me a comment and I will try to help further.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .