【发布时间】:2015-11-15 08:21:56
【问题描述】:
我有一个包含 2 个“配对”整数数组 newNumerator[] 和 newDenominator[] 的程序,它们都有 9 个整数。我编写了一个按升序对它们进行排序的函数,但是我不确定它是否有效,因为我还没有成功编译它。我也有一些类型转换的问题。这是函数定义-
void sortData(int *newNumerator[], int *newDenominator[], int newSize)
{
int temp1;
int temp2;
bool swap;
int count = 0;
double percentageLeft = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
double percentageRight = 100.0 * static_cast<double>(newNumerator[count + 1]) / newDenominator[count + 1];
do
{ swap = false;
for(count = 0; count < (newSize - 1); count++)
{
if(percentageLeft > percentageRight)
{
temp1 = *newNumerator[count];
*newNumerator[count] = *newNumerator[count + 1];
*newNumerator[count + 1] = temp1;
temp2 = *newDenominator[count];
*newDenominator[count] = *newDenominator[count + 1];
*newDenominator[count + 1] = temp2;
swap = true;
}
}
} while (swap);
}
我遇到的类型转换问题是 percentLeft 和 percentRight 因为 newNumerator[ ] 和 newDenominator[ ] 是整数指针,但要获得它们的“百分比”,我需要它是双精度的。不知道该怎么做。基本上我只需要弄清楚如何解决这个问题,并且还要知道我的功能是否达到了它的目的。任何帮助表示赞赏,如果有什么我可以进一步澄清的,请告诉我
【问题讨论】:
-
请始终在问题中包含错误消息
-
现在我得到的唯一一个是“161:38: Static_cast from 'int *' to 'double' is not allowed”
标签: c++ arrays sorting pointers casting