Hot answers tagged

4

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,...


2

1) Because I can't modify the header, how can I use strcmp (hints ask for) without string.h ? You can include string.h as per the spec: (and the inclusion of additional header files, if you’d like). (and yes, it looks like plurality.c is a typo and should read runoff.c) You cannot change to this for (int j = 0; j < 3; j++) // each voters can vote 3 ...


2

Did you watch the "functions" short in week 2? Doug mentions void at around 17:00 (the rest is also worth watching). void is a data type, just without any data. It's often used to indicate a lack of data, or in case of pointers not defining the data's type (you might find some pointers of type void* when some code does not care about the data format, but ...


2

The problem is in this loop: for (int j = 1; j < candidate_count; j++) { if (candidates[preferences[i][j]].eliminated == false) { candidates[preferences[i][j]].votes += 1; } } If your preference 0 is taken it registers all other preferences. That is to say if their are 3 candidates and preference 0 has been eliminated it will add the ...


1

I think there a problem with this line of code: if(candidates[preferences[v][r]].eliminated == false ) { candidates[preferences[v][r]].votes++; break; What happens when it finds the first choice for a candidate has been eliminated? Because it needs to check what his next choice is and so forth. Until it ether finds a ...


1

Working through your screenshots is annoying. It would be better if you posted just the relevant parts here in a code block (the {} button in the editor might help), or, if it's longer code, copy/pasted to gist.github.com, or pastebin.com, or similar, and provided a link. The number of candidates candidate_count could be anything from 1 to MAX_CANDIDATES. ...


1

The int j = 0; would have to go into the loop, as you want to start at 0 for each voter. If you fix the other part and don't reset it, and j keeps growing, you might run into segmentation faults. So your if triggers, and you advance to the second candidate on the list. But what then, what if that one has been eliminated as well? You would need some kind of ...


1

The program "never finishes" because it is stuck in an infinite loop because there is never a winner. There's a problem in find_min, it always returns 0 because this if (candidates[i].votes < min) will always be false. There are some problems in tabulate: It's not counting all the voters. In your example there are 2 candidates and 3 voters; the last ...


1

The root of the problem: add a global variable called candidates_out to keep track of the number of eliminated candidates. From the spec: you should not modify anything else in plurality.c check50 uses it's own black magic to run tests and verify results. IMO one of the important skills learned in this class is following the spec to the letter. In a ...


1

There's no need to count the candidates having min votes. If all the remaining candidates have min votes, it's a tie. Any candidate having more votes than min will tell you it's not a tie (yet).


1

candidates[i].votes is a case for the tabulate function, and should not be considered in vote, which is only meant to record a given vote. To determine the candidate with the name match, you would check strcmp(candidates[j].name, name), since your loop uses j variable to iterate over the array. And inside this if, you would have to set the correct value in ...


1

The file name would likely be a typo :) It's ok to add header includes to runoff.c. I guess that sentence got refactored a bit, and the part in parentheses should be moved to the where-to-change part to be less confusing. Each voter has candidate_count votes, for the example of three candidates, that's three votes. It's the order that matters. ...


1

Interesting that check50 doesn't pick that up! There is a problem in find_min. That function should have no side-effects, ie should not change any data, only report a result. This line candidates[j].votes = candidates[i].votes; has the potential to change a candidates vote count; that would be bad. There may be other problems in the code, but this jumps off ...


1

This routine always returns candidate 0's vote count (if not eleminated) or voter_count (the default value for min) if candidate 0 is eliminated because return min; is inside the for loop. return is the final line executed in any function.


1

One problem I see: In tabulate() for(int i = 0; i > voter_count; i++) I think you meant to write i < voter_count because otherwise it will not enter the loop since your condition will turn out to be 0 > 2 The same holds true for the next loop as well: for (int j = 0; j > candidate_count; j++). Here too, change j > candidate_count to j < ...


1

Looking at the posted code and not sure of the full context of what you're trying to do, I'll take a run at it. This is more of a question of what to take out, not what to put in. The code is a for loop, implying that it will likely repeat the loop so that it traverses the array. But it will only run once, no matter what. Here's why. No matter what ...


1

To check the name of the candidate in the 1st preferred coloumn only Remember, preferences[i][j]; Is simply, preferences[row][coloumn]; So, For first preference only preferences[changing_row][1st_coloumn]; Inside this cell is just a number, Use it to match the name of the candidate.


1

vote This function inputs a string a search for the names in the table of candidates, it returns true once the name has been founded or matched i.e == 0 But how? Use for-loop to iterate through the candidate_count and check the name of the candidate by using strcmp declared in string.h if (strcmp(candidate[i].names, name) == 0) { return true; } In this ...


1

Please share the code correctly so that I can help you as quickly as possible. Also tell a bit more about your problem Thanks.


1

Actually your approach is very good to tabulate the vote count But, Have you noticed, that you only should have to update the vote count for the candidates that has been preferred first rank, and how you can access the first preference? Remember, preferences[i][j] is basically preferences[row][coloumn] So, To count only first ranked candidates you should do, ...


1

Just got tabulate to work with this code! void tabulate(void) { for (int k = 0; k < voter_count; k++) { if (candidates[preferences[k][0]].eliminated == false) { candidates[preferences[k][0]].votes++; } else if (candidates[preferences[k][0]].eliminated == true) { candidates[preferences[...


1

Just add one for loop and loop till the candidate_count If name and candidates[i].name matches then you should assign the i value to preferences[][] remember preferences[0][0] means that 1st voter has given 1st rank to the candidate no [i].....


1

rank++ This means "tell me the value of rank, then add one to rank". So in the last line of elim_test, you're calling elim_test again with the exact same values over and over again. To fix this, you can either use ++rank, which means "add one to rank, then tell me the value of rank", or you can write elim_test(voter, rank + 1);, since ...


1

the function you wrote will return 0 back to the main function regardless of what values you've assign to other variables within the find_min function.


1

So I got the answer. My problem was the candidates[i].eliminated == false. Very easy to see, I don't know how I missed it. The candidates[i].eliminated == false should go with the first if function. If the candidate is eliminated, why would you even need to add their value to h? It should just be excluded at the initial stage.


1

check50 is seeing if you print the candidate's name, then a newline. You're printing the candidate's name, then a space, then a newline. The formatting is pretty finicky, so you'll need to remove the space.


1

When in doubt, read the doc! From man printf Description The functions in the printf() family produce output according to a format as described below. The functions printf() and vprintf() write output to stdout, the standard output stream; fprintf() and vfprintf() write output to the given output stream; sprintf(), snprintf(), vsprintf() and ...


1

In the print_winner function when using printf function i didnt insert \n to print newline. Additionally, for the find_min function, i initialise min = candidates[0] votes which is not very correct, because if candidate[0] was eliminated, min will become 0. So instead changed to int min = voter_count!


1

The test condition must be true for the loop to run. Perhaps it should be n < candidate_count - 1;? If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

Each round counts one vote per voter, for their top preferred, non-eliminated candidate. Once that vote is counted, program moves on to the next voter. From the C99 standard section 6.8.6.3: A break statement terminates execution of the smallest enclosing switch or iteration statement. In this case it terminates the the j loop and moves on to the ...


Only top voted, non community-wiki answers of a minimum length are eligible