Hi if anyone can guide me in the right direction to record name & rank in the preferences array? I don't have any clue how to even utilize the int vote and int rank in the bool parentheses of the function. Just point me in the right direction, I don't know how to record it in the preferences array. TIA <3

bool vote(int voter, int rank, string name)


for(int i = 0; i < voter_count; i++)
    for (int j = 0; j < candidate_count; j++)
        if(strcmp(candidates[j].name, name) == 0)
            return true;
return false;

Writing it this won't work either

preferences[i][j] = candidates[j].name;

compiler gives error that

runoff.c:138:35: error: incompatible pointer to integer conversion assigning to 'int' from 'string' (aka 'char *') [-Werror,-Wint-conversion] preferences[i][j] = candidates[j].name;



Recall the definition of our preferences array:

Next up is a two-dimensional array preferences. The array preferences[i] will represent all of the preferences for voter number i, and the integer preferences[i][j] here will store the index of the candidate who is the jth preference for voter i.

Those i and j are not necessarily the i and j from your loops, but the voter number and the rank, whatever name those would have in your code. The definition of the vote function tells you those are passed as parameters, you would use those, not loop variables for the index to preferences.

Also, the value to be stored is the index of the candidate, not their name.

  • preferences[voter][rank] = i; I seem to have fixed my code with this, but someone told me there is no need for two loops and a single will do. But here, there are two variables in the preferences"voter"&"rank equals i'th value, so my question is how compiler assumes the right variable for the i'th value?
    – yoyo Ahmed
    Feb 3 '20 at 5:19
  • Nobody tells you to use two loops. Both indices to preferences are already given by the function parameters, the only loop making sense is the one searching for a name match. If you have another loop, chances are you are telling the computer to do the same work multiple times.
    – Blauelf
    Feb 3 '20 at 7:02
  • Any idea how compiler knows here Alice is the first preference of the voter? Please guide next how do I update that in the preference? Thanks
    – yoyo Ahmed
    Feb 3 '20 at 7:51
  • The compiler doesn't know Alice. It's your job to find out which candidate index Alice has. The main function will call the function with a voter number, the rank (zero-based index), and a name that voter wrote down in that field. Your function should only handle that single preference at a time.
    – Blauelf
    Feb 3 '20 at 8:20
  • To my understanding, preferences[vote][rank] = i is updating the rank, but in debug50 it doesn't show the candidate no. which is being assigned with the rank. if first voter put Alice as first preference then the second voter put Alice as the second preference, will the compiler know the candidate no. assigned to Alice? I am having a hard time understanding this. Please guide me.
    – yoyo Ahmed
    Feb 3 '20 at 11:03

You must log in to answer this question.

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