gpt4 book ai didi

c - 对指针的修改改变了输出

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:45 24 4
gpt4 key购买 nike

我试图使以下代码对 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;
}

Ballot 的结构只是一个指向表示选票的整数数组的指针。

struct Ballot {
int *votes;
};

重要的是它按预期工作,但如果需要,我会解释或发布辅助函数(例如 Argmax 或 Print_Results)的代码。以下是我如何修改这些功能并以某种方式设法破坏功能。我将一些内容更改为指针或双指针并执行了代码,但结果与我已验证为正确的第一个版本有很大不同。问题似乎出在 Tally_Borda 身上,因为我打印了 Results 第一行(候选)的结果,看看它们是否匹配,但不匹配。

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

最佳答案

new_Borda_Count 中的这一行对我来说意义不大:

        Results[i] = calloc(1, BALLOT_SIZE);

calloc 的第一个参数是元素的数量,第二个参数是元素的大小。

这样,

        Results[i] = calloc(BALLOT_SIZE, sizeof *Results[i]);

对我来说似乎更明智,因为第一个示例代码中的 ResultsBALLOT_SIZE x BALLOT_SIZE

的二维数组

关于c - 对指针的修改改变了输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45070898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com