-1

I am undertaking the CS50 course and I came across this issue in problem set 3 with Tideman voting system. My question is not about the algorithm itself but with the variable ranks where it is only defined within the main function.

For the solution you can only update the TODO functions and nothing else. Also as part of the solution the ranks array has to be updated within the vote function. My problem is to understand how is the variable accessed from another function when it's not globally defined?

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

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: tideman [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] = argv[i + 1];
    }

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
    // TODO
    
    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // TODO
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO
    return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO
    return;
}

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    // TODO
    return;
}

// Print the winner of the election
void print_winner(void)
{
    // TODO
    return;
}

MoZed
  • 1
  • 1
  • 1
    The variable is passed as an argument to the function. *ranks* is an array defined in the main function, then this array is passed to *vote*, *record_preferences*, and so on. – Berthin Jan 30 '21 at 22:24
  • https://stackoverflow.com/questions/2229498/passing-by-reference-in-c – 0___________ Jan 30 '21 at 22:27
  • I am putting an effort myself and I'm doing a course. I have searched for arrays but couldn't find anything related Also don't think this is a pointers issue or at least not in a form that the link above shows. – MoZed Jan 30 '21 at 22:43
  • @MoZed : You need to hone your Google-Fu: https://beginnersbook.com/2014/01/c-passing-array-to-function-example/ – Clifford Jan 30 '21 at 22:45
  • Please take the [tour] and read [ask]. Your title and the only question you ask in the content of your post, aren't related to each other. This line is where it is passed: `if (!vote(j, name, ranks))` btw. – jwdonahue Jan 30 '21 at 22:46

1 Answers1

1

When you pass an array as a function parameter, it is passed by reference not by copy. So when you access ranks in vote(), you are accessing the instance of the array in main().

Clifford
  • 88,407
  • 13
  • 85
  • 165