c++ 用了double数字仍是整数,我要求一组数的平均值,但总是给我整数而不是我想要的小数,该怎么解决呢?

double averageScore(int array[], int size)
{
int total = 0; //To hold the total value
double average = 0;//To hold the average value

//Calculate the total number of this array
//for adding all these numbers together.
for (int count = 0; count < size; count++)
total = array[count] + total;

average = total / size; //Calculate the average number based on the total numbers and the size of the array

cout << "The average number in this array is: " << average << endl;
//Return the average number
return average;
}

把计算平均值哪一行修改一下:
average = total*1.0 / size;
因为在C、C++中,两个整数相除,结果永远为整数(向下取整),如果想让计算结果为浮点数,则参与运算的被除数和除数至少有一个得是浮点数。
温馨提示:答案为网友推荐,仅供参考
相似回答