0

I'm having trouble understanding how to write this vote function. I understand what I wrote is incorrect because I am assigning a struct to an array of ints but I can't understand how I'm supposed to tell preferences that candidates[i] is the voters choice.

bool vote(int voter, int rank, string name)
{
    // Loop over size of candidate list
    for (int i = 0; i < candidate_count; i++)
    {
        // Compare two strings (strcmp returns 0 if there's a match)
        if(strcmp(name, candidates[i].name) == 0)
        {
            preferences[voter][rank] = candidates[i];
            return true;
        }
    }
    return false;
}

error: assigning to 'int' from incompatible type 'candidate' preferences[voter][rank] = candidates[i];

The only int in the candidate struct is votes which I don't think is useful for understanding which candidate was picked.

preferences[voter][rank] = candidates[i].votes;

I'd appreciate any help in understanding the intuition behind this problem. Thank you!

1
  • You should look at the declaration of preferences again. What are the two variables used to declare it's size?
    – Code_Newty
    Feb 10 at 17:04

2 Answers 2

1
/* The index of the candidate gives the information about the voter's choice. With the index known the voter's choice is also known. So basically, store the index i of the inner loop i.e. */

preferences[voter][rank] = i;
0

From the spec [emphasis added]:

the integer preferences[i][j] here will store the index of the candidate who is the jth preference for voter i

You must log in to answer this question.

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