0

I am having real difficulty understanding how calling the vote function for valid votes in runoff is supposed to work.

The relevant part of the distribution code is as follows:

// Record vote, unless it's invalid     
if (!vote(i, j, name))
            {
                printf("Invalid vote.\n");
                return 4;
            }

I understand that the vote function is supposed to update the preferences array to record each voter's preference.

I also understand that the exclamation (!) before vote in the code above means that if the vote function returns false then it's an invalid vote and prints the 'Invalid vote' error message.

What I don't understand is how the above code records a valid vote (per the comment at the top of the snippet). To me the only thing that happens in the above code is to print out 'Invalid Vote' if vote returns false.

What happens if vote returns true? I would have thought we would need to add additional line(s) of code to deal with that situation, but per the spec we are not expected to edit anything other than the functions. Is it implicit in the above code that if vote returns true then the vote function runs as normal and updates the preferences array?

Ironically I am having more difficulty grasping the mechanics of the function call on a conceptual level than I am coding the actual vote function itself.

Thanks very much in advance for any clarification.

1

"Is it implicit in the above code that if vote returns true then the vote function runs as normal and updates the preferences array?" Answer: No.

The vote function runs and then the result is evaluated. If vote returns true, the function has already updated the preferences array (assuming, of course, it's been written correctly :)

You must log in to answer this question.

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