Home > front end >  Why does the INT Rank change, and why does it not override? I don't see it updating or any code
Why does the INT Rank change, and why does it not override? I don't see it updating or any code

Time:11-02

I'm doing CS50 and I am currently at the Tideman problem. Here we have to write a function code that completes the vote function. When I do check my answers for the funtion it does work, although I don't completely understand how it does it.

I tried to debug it and see how the values change. Let me be clear it is about the vote funtion, and the part I don't understand is the INT vote. Why does it change value?

#include <cs50.h>
#include <stdio.h>
#include <string.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 (hierin werken)
bool vote(int rank, string name, int ranks[])
{
    printf("Rank begin snap niet %i \n", rank);

    for (int i = 0; i < candidate_count; i  )
    {
        if (i == 3)
        {
            printf("0: %i 1: %i 2: %i", ranks[0], ranks[1], ranks[2]);
        }
        {
            printf("Array rank %i \n", ranks[i]);
        }
        if (strcmp(candidates[i], name)==0)
        {
            ranks[rank] = i;
            return true;
        }


    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // for (int i = 0; i < candidate_count; i  )
    // if (ranks[i] < candidates[i])
    //     {
    //         int preferences[i][i] = ranks[i];
    //     }

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

CodePudding user response:

o you understand how, if you have int array[10]; int j = 5; array[j] = 42;, then after the assignment, array[5] == 42? The same principles apply to ranks[rank] = i;. After the assignment, the element of the array ranks identified by rank contains the value in i at the time of the assignment. – Jonathan Leffler 5 hours ago

Do you understand the array, j, and array[j] = 42 example? It is isomorphic with the ranks[rank] = i example. That is: array and ranks are both arrays of integers; j and rank are both indexes into the corresponding array; and 42 and i are the values assigned to the element of the array. The 'at the time of the assignment' qualifier is because if i changes after the assignment, the value in ranks[rank] doesn't change. – Jonathan Leffler 5 hours ago

  • Related