我的程序中是否存在计算错误?

Lea*_*ner 13 c++ function

显然在我的程序中某处存在计算错误,但我根本找不到它.

我有关于计算错误原因的唯一信息是MyProgrammingLab给出的以下反馈(一个自动测试代码以查看它是否不正确的站点).我不知道为年度死亡率和年出生率输入了什么值.可能是我说得对,但MyProgrammingLab错了吗?老实说,我所有的测试似乎都很好.

预期产出:

Year 1: 200 176
Year 2: 176 154
Year 3: 154 135
Year 4: 135 118
Year 5: 118 103
Year 6: 103 90
Year 7: 90 79
Run Code Online (Sandbox Code Playgroud)

实际产量:

Year 1: 200 199
Year 2: 199 198
Year 3: 198 197
Year 4: 197 196
Year 5: 196 195
Year 6: 195 194
Year 7: 194 193
Run Code Online (Sandbox Code Playgroud)

我根据以下任务构建了程序:

在人口中,出生率是出生人口增加的百分比,死亡率是死亡人口减少的百分比.编写一个要求以下内容的程序:

  • 人口的起始规模(最小2)(提示Enter starting size:)
  • 年出生率(提示Enter annual birth rate:)
  • 年死亡率(提示Enter annual death rate:)
  • 显示的年数(最小1)(提示Enter years to display:)

然后,该计划应显示每年年底的起始人口和预计人口.它应该使用一个函数来计算并返回一年后人口的预计新大小.公式是

  N = P(1 + B)(1 - D) 
Run Code Online (Sandbox Code Playgroud)

其中N是新的人口规模,P是先前的人口规模,B是出生率,D是死亡率.年出生率和死亡率是每1000人一年中的典型出生和死亡人数,以小数表示.因此,例如,如果特定人群中每1000人通常有大约32个新生儿和26个死亡,则出生率为.032,死亡率为.026.

我的代码:

#include <iostream>
using namespace std;

int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {
    float annualBirthRate2 = annualBirthRate / 1000;
    float annualDeathRate2 = annualDeathRate / 1000;

    int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);
    return newpopulation;
}

int main() {

    int populationStartingSize = 0;
    float annualBirthRate = 0;
    float annualDeathRate = 0;
    int numberOfYearsToDisplay = 0;

    do  {
        cout << "Enter starting population size: ";
        cin >> populationStartingSize;
        cout << "Enter annual birth rate: ";
        cin >> annualBirthRate;
        cout << "Enter annual death rate: ";
        cin >> annualDeathRate;
        cout << "Enter years to display: ";
        cin >> numberOfYearsToDisplay;
    } while (!(populationStartingSize >= 2) || !(numberOfYearsToDisplay >= 1));

    int population;

    for (int i = 1; i <= numberOfYearsToDisplay; i++) {
        cout << "Year " << i << ": " << populationStartingSize << " ";
        population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
        cout << population << endl;
        populationStartingSize = population;
    }

    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Rey*_*esh 3

所以,答案是

不需要将annualBirthRate和annualDeathRate除以1000。由于annualBirthRate是按每1000人的年出生人数计算的,因此不需要再次除以1000。

因此删除这些行

float annualBirthRate2 = annualBirthRate / 1000;
float annualDeathRate2 = annualDeathRate / 1000;
Run Code Online (Sandbox Code Playgroud)

和改变

int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);
Run Code Online (Sandbox Code Playgroud)

int newpopulation = population * (1 + annualBirthRate) * (1 - annualDeathRate);
Run Code Online (Sandbox Code Playgroud)

所以,最终的代码将如下所示:

#include <iostream>
using namespace std;

int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {

    int newpopulation = population * (1 + annualBirthRate) * (1 - annualDeathRate);
    return newpopulation;
}

int main() {

    int populationStartingSize = 0;
    float annualBirthRate = 0;
    float annualDeathRate = 0;
    int numberOfYearsToDisplay = 0;

    do  {
        cout << "Enter starting population size: ";
        cin >> populationStartingSize;
        cout << "Enter annual birth rate: ";
        cin >> annualBirthRate;
        cout << "Enter annual death rate: ";
        cin >> annualDeathRate;
        cout << "Enter years to display: ";
        cin >> numberOfYearsToDisplay;
    } while (!(populationStartingSize >= 2) || !(numberOfYearsToDisplay >= 1));

    int population;

    for (int i = 1; i <= numberOfYearsToDisplay; i++) {
        cout << "Year " << i << ": " << populationStartingSize << " ";
        population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
        cout << population << endl;
        populationStartingSize = population;
    }

    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

你们在评论区讨论了,但没有回答这个问题。

  • @BenjaminLindley 那又怎样..?不回答问题也是不正确的..它出现在未回答的列表中..回答问题没有任何错误.. (2认同)