gpt4 book ai didi

C++ 编程家庭作业(do...while 循环)

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:48 25 4
gpt4 key购买 nike

我的一个编程作业如下:

The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both the towns at that time. (A sample input is: Population of town A = 5000, growth rate of town A = 4%, population of town B = 8000, and growth rate of town B = 2%.)

我把作业的主要部分写下来,输入人口和增长率并计算 x 年后的人口,但我不知道如何比较 A 镇的最终人口与 B 镇的最终人口和总和。任何提示将不胜感激。

#include <iostream>

using namespace std;

int main()
{
int popA, popB, year = 1;
double growth_rateA, growth_rateB;

cout << "Enter the population and growth rate of Town A: ";
cin >> popA >> growth_rateA;
cout << endl;

cout << "Enter the population and growth rate of Town B: ";
cin >> popB >> growth_rateB;
cout << endl;

if (popA < popB && growth_rateA > growth_rateB)
{
{
do {
(popA = ((growth_rateA / 100) * popA) + popA); // calculates population growth in one year
(popB = ((growth_rateB / 100) * popB) + popB);
year++;
}

while (popA < popB);

cout << "Town A will surpass Town B in population after " << year << " years.\n" << endl;
cout << "The final population of Town A is: " << popA << ".\n" << endl;
cout << "The final population of Town B is: " << popB << ".\n" << endl;
}
}
else
{
cout << "Invalid Data.";
}

system("pause");
return 0;
}

最佳答案

Bettorun 说:因为 popApopB 被声明在 outside do-while 循环,它们将在您退出该循环后保留其最终值,您可以使用它们对它们执行其他计算。

这是我添加的代码。我还去掉了一层不必要的嵌套,并从 0 而不是 1 开始这一年。

#include <iostream>

using namespace std;

int main()
{
int popA, popB, year = 0;
double growth_rateA, growth_rateB;

cout << "Enter the population and growth rate of Town A: ";
cin >> popA >> growth_rateA;
cout << endl;

cout << "Enter the population and growth rate of Town B: ";
cin >> popB >> growth_rateB;
cout << endl;

if (popA < popB && growth_rateA > growth_rateB) {
do {
(popA = ((growth_rateA / 100) * popA) + popA); // calculates population growth in one year
(popB = ((growth_rateB / 100) * popB) + popB);
year++;
} while (popA < popB);

int popDifference = popA - popB;
int popTotal = popA + popB;
cout << "Town A will surpass Town B in population after " << year << " years.\n" ;
cout << "The final population of Town A is: " << popA << ".\n";
cout << "The final population of Town B is: " << popB << ".\n";
cout << "In year " << year << ", Town A has " << popDifference << " more people than town B.\n";
cout << "The total population of both Town A and Town B is " << popTotal << ".\n";
} else {
cout << "Invalid Data.";
}

system("pause");
return 0;
}

关于C++ 编程家庭作业(do...while 循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com