I have completed and submitted the easier version of the problem set for the week I'm on, I'm just interested in finishing the harder version for the learning experience, but I'm stuck on what seems like the crux of it. Basically you are supposed to implement a voting system that records all the ranks of each of the voters. So for instance let's say you have 3 candidates (A, B, and C). If the first voter chooses candidate C as their first choice, and B as their second, (A as their last), the orginal array you would have looks like this [2,0,1] signifying that the the third candidate is the first choice, the first candidate is the second choice, and the second candidate is the third choice. The code I've used for this is the following:
bool vote(int rank, string name, int ranks[]) { for (int i = 0; i < candidate_count; i++) { if(strcmp(candidates[i], name) == 0) { ranks[rank] = i; for( int j = 0; j < candidate_count; j++) { printf("%i\n", ranks[j]); } return true; } } return false; }
But then we are asked to convert it into a 2d array that compares each of the candidates in basically a 1vs1 battle. So in our ABC example, the array should convert from [2,0,1] to [[0,1,0],[0,0,0],[1,1,0]] where the first array represents the first candidate and how they did against the 2nd and 3rd respectively, etc. (the first location in the first array should always be 0 because you cant compare the candidate to how many votes they had against themself, same with the middle spot in the second array and the last spot in the last array). In other words array[i][j] represents how candidate i did against candidate j. You are meant to work this function out using the array returned from the vote function.
I know that it will involve another nested loop, possibly 3 layers. I need a point in the right direction. I've made a bunch of different tweaks to a function that looks like this but I know they've all been wrong because I'm resorting to trial and error rather than logic because the logic has defeated me at this point. Maybe this forum isn't really meant for logical help but I'd still like to work this out on my own without being given the answer. Anyway here's the latest version of the function that I've been helplessly tinkering with.
void record_preferences(int ranks[]) { printf("\n"); for (int i = 0; i < candidate_count; i++) { for (int j = 0; j < candidate_count; j++) { for (int k = 0; k < candidate_count; k++) { if((ranks[k] > i && ranks[k] < j) || (ranks[k] < i && ranks[k] > j)) { preferences[i][j] += 1; } } printf("%i\n", preferences[i][j]); } } return; }
Keep in mind I've already gotten my grade for this week and I don't plan on submitting this work so it's not cheating even if you do straight up tell me the answer, but I'd prefer if you didn't. I know a lot of people are of the belief that you need to work the logic out yourself otherwise you're not really learning, which I kind of get, but at the same time you can only bang your head against a wall so many times before seeking help. Thanks.
https://stackoverflow.com/questions/66897643/cs50-tideman-converting-information-from-one-array-into-another-larger-one April 01, 2021 at 10:20AM
没有评论:
发表评论