You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++计算月均降雨量时double变量自动取整问题求助

问题:计算月均降雨量时结果被自动取整

我是C++新手,尝试通过总降雨量除以12个月计算月均降雨量,但发现平均值总是自动取整。已检查所有变量并调试计算过程,发现total/12的结果为取整后数值,恳请告知问题所在并给出解决建议。

原代码

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;

void assignMonths(string monthsName[SIZE], int rainFall[SIZE]);
void calRainfall(int rainFall[SIZE], int&, double&);
void displayValues(string monthsName[SIZE], int rainFall[SIZE], int total, double average);
int main()
{

    string monthsName[SIZE];
    int rainFall[SIZE];
    int total = 0;
    double average = 0;

    assignMonths(monthsName, rainFall);
    calRainfall(rainFall, total, average);
    displayValues(monthsName, rainFall, total, average);

    return 0;
}

void assignMonths(string months[SIZE], int rain[SIZE])
{
    const string MONTHS[SIZE] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    srand(time(0));

    for (int i = 0; i < SIZE; i++) {
        months[i] = MONTHS[i];
        rain[i] = rand() % 100;
    }
    return;
}

void calRainfall(int rain[SIZE], int& total, double& average)
{
    for (int i = 0; i < SIZE; i++) {
        total = total + rain[i];
    }
    average = total / SIZE;
    return;
}

void displayValues(string months[SIZE], int rain[SIZE], int total, double average)
{

    cout << "Rainfall per Month (in inches)" << endl;
    cout << endl;
    //Display all the values
    for (int i = 0; i < SIZE; i++) {
        cout << setw(30) << left << months[i] << right << rain[i] << endl;
    }
    cout << endl
         << setw(30) << left << "Total Rainfall: " << right << total << endl;
    cout << fixed << showpoint << setprecision(4) << setw(30) << left << "Average Rainfall:" << right << average << endl;
    return;
}

问题原因

核心问题是整数除法的特性:C++中,当两个整数类型的操作数进行除法运算时,编译器会执行整数除法,直接舍弃结果的小数部分,只保留整数商。你的代码里total是int类型,SIZE是整数常量(代表12),所以total / SIZE的结果是整数,之后再赋值给double类型的average也无法恢复已经丢失的小数部分。

另外,代码中未定义SIZE常量,这会导致编译错误,需要补充定义。

解决建议

  1. 触发浮点除法:将除法操作中的任意一个操作数转换为浮点类型,强制编译器执行浮点除法,保留小数部分。常见的两种方式:
    • 把total强制转换为double:average = static_cast<double>(total) / SIZE;
    • 直接使用浮点数值作为除数:average = total / 12.0;(因为12.0是double类型)
  2. 补充SIZE常量定义:在全局作用域或main函数之前添加const int SIZE = 12;,解决编译报错问题。

修改后的完整代码

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;

const int SIZE = 12; // 补充定义SIZE常量

void assignMonths(string monthsName[SIZE], int rainFall[SIZE]);
void calRainfall(int rainFall[SIZE], int&, double&);
void displayValues(string monthsName[SIZE], int rainFall[SIZE], int total, double average);
int main()
{

    string monthsName[SIZE];
    int rainFall[SIZE];
    int total = 0;
    double average = 0;

    assignMonths(monthsName, rainFall);
    calRainfall(rainFall, total, average);
    displayValues(monthsName, rainFall, total, average);

    return 0;
}

void assignMonths(string months[SIZE], int rain[SIZE])
{
    const string MONTHS[SIZE] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    srand(time(0));

    for (int i = 0; i < SIZE; i++) {
        months[i] = MONTHS[i];
        rain[i] = rand() % 100;
    }
    return;
}

void calRainfall(int rain[SIZE], int& total, double& average)
{
    for (int i = 0; i < SIZE; i++) {
        total = total + rain[i];
    }
    // 修改为浮点除法
    average = static_cast<double>(total) / SIZE;
    return;
}

void displayValues(string months[SIZE], int rain[SIZE], int total, double average)
{

    cout << "Rainfall per Month (in inches)" << endl;
    cout << endl;
    //Display all the values
    for (int i = 0; i < SIZE; i++) {
        cout << setw(30) << left << months[i] << right << rain[i] << endl;
    }
    cout << endl
         << setw(30) << left << "Total Rainfall: " << right << total << endl;
    cout << fixed << showpoint << setprecision(4) << setw(30) << left << "Average Rainfall:" << right << average << endl;
    return;
}

内容的提问来源于stack exchange,提问作者Tobi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:11:26