我试图让以下代码对BALLOT_SIZE的大输入值更友好,但遇到了问题。
void Dot_Product(int a[BALLOT_SIZE][BALLOT_SIZE], int output[BALLOT_SIZE]) {
for (int i = 0; i < BALLOT_SIZE; i++) {
int total = 0;
for (int j = 0; j < BALLOT_SIZE; j++) {
total = total + a[i][j] * (BALLOT_SIZE - (j+1));
}
output[i] = total;
}
}
void Tally_Borda(int Results[BALLOT_SIZE][BALLOT_SIZE], struct Ballot * Ballots[NUM_VOTERS]) {
for (int i = 0; i < NUM_VOTERS; i++) {
for (int j = 0; j < BALLOT_SIZE; j++) {
Results[j][Ballots[i]->votes[j]-1]++;
}
}
}
int Borda_Count(struct Ballot * Ballots[NUM_VOTERS]) {
printf("\nBorda Count:\n");
int Results[BALLOT_SIZE][BALLOT_SIZE] ={{0}};
Tally_Borda(Results, Ballots);
Print_First_Candidate(Results[0], BALLOT_SIZE);
int DotResult[BALLOT_SIZE];
Dot_Product(Results, DotResult);
Print_Results(DotResult, BALLOT_SIZE);
int finalResult = Argmax(DotResult, BALLOT_SIZE);
printf("%d is the Borda Count winner\n", finalResult);
return finalResult;
}
选票的结构只是指向一组代表投票的整数的指针。
struct Ballot {
int *votes;
};
重要的是,这可以按预期工作,但如果需要,我将解释或发布帮助函数的代码(例如Argmax或Print_Results)。以下是我如何修改这些功能并以某种方式设法破坏功能。我将一些东西改为指针或双指针并且代码执行,但是与第一个版本的结果非常不同,我已经验证了它是正确的。似乎问题在于Tally_Borda,因为我打印结果的第一行(候选)的结果,看看它们是否匹配,但它们不匹配。
void new_Dot_Product(int * a[BALLOT_SIZE], int output[BALLOT_SIZE]) {
for (int i = 0; i < BALLOT_SIZE; i++) {
int total = 0;
for (int j = 0; j < BALLOT_SIZE; j++) {
total = total + a[i][j] * (BALLOT_SIZE - (j+1));
}
output[i] = total;
}
}
void new_Tally_Borda(int *Results[BALLOT_SIZE], struct Ballot * Ballots[NUM_VOTERS]) {
for (int i = 0; i < NUM_VOTERS; i++) {
for (int j = 0; j < BALLOT_SIZE; j++) {
Results[j][Ballots[i]->votes[j]-1]++;
}
}
}
int new_Borda_Count(struct Ballot * Ballots[NUM_VOTERS]) {
int ** Results = malloc(BALLOT_SIZE*sizeof(int*));
for (int i = 0; i < BALLOT_SIZE; i++) {
Results[i] = calloc(1, BALLOT_SIZE);
}
new_Tally_Borda(Results, Ballots);
Print_First_Candidate(Results[0], BALLOT_SIZE);
int * DotResult = malloc(BALLOT_SIZE * sizeof(int));
new_Dot_Product(Results, DotResult);
Print_Results(DotResult, BALLOT_SIZE);
int finalResult = Argmax(DotResult, BALLOT_SIZE);
printf("%d is the Borda Count winner\n", finalResult);
return finalResult;
}
我错过了什么?
在下面的示例中,选民人数为1000,选票大小为15,因此原始的borda计数是正确的,因为第一个候选人的选票总数为1000.
示例输出:
new_Borda Count:
First Candidate:
Position 1: 50
Position 2: 56
Position 3: 64
Position 4: 75
Position 5: 137
Position 6: 142
Position 7: 142
Position 8: 143
Position 9: 190
Position 10: 201
Position 11: 196
Position 12: 204
Position 13: 281
Position 14: 267
Position 15: 267
Totals:
Candidate 1: 12176
Candidate 2: 19270
Candidate 3: 24010
Candidate 4: 26582
Candidate 5: 26043
Candidate 6: 26198
Candidate 7: 27427
Candidate 8: 26731
Candidate 9: 26693
Candidate 10: 26525
Candidate 11: 27081
Candidate 12: 26318
Candidate 13: 69270
Candidate 14: 444370
Candidate 15: 1242793
15 is the Borda Count winner
Borda Count:
Position 1: 50
Position 2: 56
Position 3: 64
Position 4: 75
Position 5: 70
Position 6: 64
Position 7: 73
Position 8: 77
Position 9: 72
Position 10: 79
Position 11: 63
Position 12: 65
Position 13: 55
Position 14: 59
Position 15: 78
Totals:
Candidate 1: 6863
Candidate 2: 7134
Candidate 3: 7045
Candidate 4: 7129
Candidate 5: 6711
Candidate 6: 6879
Candidate 7: 7069
Candidate 8: 6922
Candidate 9: 7100
Candidate 10: 7044
Candidate 11: 7153
Candidate 12: 6967
Candidate 13: 7027
Candidate 14: 6928
Candidate 15: 7029
11 is the Borda Count winner
答案 0 :(得分:2)
new_Borda_Count
中的这一行对我来说没有多大意义:
Results[i] = calloc(1, BALLOT_SIZE);
calloc
的第一个参数是元素的数量,第二个参数是元素的大小。
有了这个,
Results[i] = calloc(BALLOT_SIZE, sizeof *Results[i]);
对我来说似乎更明智,因为您的第一个示例代码中的Results
是BALLOT_SIZE
x BALLOT_SIZE