else if (order == "average") {
int average = 0;
int temp;
for (int i = 0; i < NO_TEAMS; i++)
{
for (int j = 0; j < NO_TEAMS - 1; j++)
{
if (ptr[yearIndex].teams[j].fum < ptr[yearIndex].teams[j + 1].fum) {
temp = ptr[yearIndex].teams[i].fum;
ptr[yearIndex].teams[j].fum = ptr[yearIndex].teams[j + 1].fum;
ptr[yearIndex].teams[j + 1].lost = temp;
}
}
}
for (int i = 0; i < NO_TEAMS; i++) {
average = average + ptr[yearIndex].teams[i].fum;
}
对于此代码,平均值返回26.但是,如果我消除了冒泡排序代码,则平均值返回20(这是正确的平均值)。我该如何解决这个问题?
答案 0 :(得分:1)
显然,你没有正确地交换物品。 问题出在以下三个方面:
function sql_formater(){
$sql = $this->input->post('query');
return $sql;
}
将第一行更改为temp = ptr[yearIndex].teams[i].fum;
ptr[yearIndex].teams[j].fum = ptr[yearIndex].teams[j + 1].fum;
ptr[yearIndex].teams[j + 1].lost = temp
,而不是j
i