C++ C++;蟑螂计划。。。灭绝功能有缺陷
我几乎完成了这个程序,但在C++ C++;蟑螂计划。。。灭绝功能有缺陷,c++,C++,我几乎完成了这个程序,但在main中有些不对劲。以下是项目说明: 作业: 编写一个程序,在几个星期内跟踪两个相邻房屋中的蟑螂数量。房屋内蟑螂的数量将由以下因素决定: 每家每户的蟑螂数量是10到100之间的随机数 每周,蟑螂的数量增加30% 这两所房子共用一堵墙,蟑螂可以通过墙从一间迁移到另一间。在给定的一周内,如果一所房子的蟑螂比另一所房子的多,那么蟑螂会从人口较多的房子迁移到人口较少的房子。具体来说,人口差异的30%(向下舍入)是移民 每四周,一名灭虫员就会造访其中一所房屋,从而使该房屋内的蟑
main
中有些不对劲。以下是项目说明:
作业:编写一个程序,在几个星期内跟踪两个相邻房屋中的蟑螂数量。房屋内蟑螂的数量将由以下因素决定:
#include <iostream>
#include <cmath>
using namespace std;
int house, increase, roaches, filthyBeasts; // My variables for my four functions
int initialCount(int house);
int weeklyIncrease(int increase);
int roachesMigration(int more, int fewer, int change);
int exterminationTime (int filthyBeasts);
// My four function prototypes
int main()
{
int houseA, houseB;
houseA = initialCount(houseA); //Initializing the inital count of House A.
houseB = initialCount(houseB); //Initializing the inital count of House B.
int week = 0;
for (week = 0; week < 11; week++) // My for loop iterating up to 11 weeks.
{
houseA = weeklyIncrease(houseA);
houseB = weeklyIncrease(houseB);
cout << "For week " << week << ", the total number of roaches in House A is " << houseA << endl;
cout << "For week " << week << ", the total number of roaches in House B is " << houseB << endl;
if((houseA > houseB)) // Migration option 1
{
houseB = roachesMigration(houseA, houseB);
}
else if((houseB > houseA)) // Migration option 2
{
houseA = roachesMigration(houseA, houseB);
}
if ((week + 1) % 4 == 0) // It's extermination time!
{
if ((rand() % 2) == 0) // Get a random number between 0 and 1.
{
houseB = exterminationTime(houseB);
}
else
{
houseA = exterminationTime(houseA);
}
}
}
return 0;
}
int initialCount(int house) // Initializing both houses to random numbers between 10 and 100.
{
int num;
num = (rand() % 91) + 10;
return num;
}
int weeklyIncrease(int increaseHouses) // Increasing the roaches in both houses by 30% weekly.
{
int increase = 0;
increase = (increaseHouses * .3) + increaseHouses;
return increase;
}
int roachesMigration(int more, int fewer, int change)
{
more -= change;
fewer += change;
return ((more - fewer) * .3);
}
int exterminationTime(int filthyBeasts) // Getting rid of the filthy little beasts!
{
filthyBeasts = (filthyBeasts * .1);
return filthyBeasts;
}
#包括
#包括
使用名称空间std;
内部房屋,增加,蟑螂,肮脏的野兽;//我的四个函数的变量
int初始计数(int house);
int周增长(int增加);
int roachesMigration(int more,int less,int change);
int消灭时间(int肮脏野兽);
//我的四个功能原型
int main()
{
内特豪斯A,豪斯B;
houseA=initialCount(houseA);//初始化houseA的初始计数。
houseB=initialCount(houseB);//初始化houseB的初始计数。
整周=0;
for(week=0;week<11;week++)//我的for循环迭代时间长达11周。
{
houseA=每周增加(houseA);
houseB=每周增加(houseB);
cout提示:我注意到您在for循环的末尾打印了week
的值。换句话说,您的输出应该类似于:
For week 0, the total number of roaches in House A is 4
For week 0, the total number of roaches in House B is 5
0
For week 1, the total number of roaches in House A is 6
For week 1, the total number of roaches in House B is 7
1
然而,我怀疑打印的周数并不是它们应该的。此外,你的蟑螂迁移图是错误的。它应该改变两个房子里蟑螂的数量,而不是蟑螂的总数。它改变了总数,但只改变了其中一个房子里蟑螂的数量
要更改两个蟑螂计数,您可以
让函数仅计算迁移的蟑螂数量,并在主循环中进行加法/减法运算
让函数更改这两个值,这需要将它们作为引用或指针传递(我建议在这里使用引用)
对于第一种选择:
int roachesMigration(int more, int fewer)
{
return ((more - fewer) * 3) / 10;
}
第二点:
void roachesMigration(int & more, int & fewer)
{
int migration = ((more - fewer) * 3) / 10;
more -= migration;
fewer += migration;
}
如果本课程中尚未涉及引用和指针,请使用第一个选项。仔细查看变量“week”的使用情况。尤其是当它改变值时
编辑:Daniel在我键入时扩展了他的答案(或者我没有完全阅读),因此我删除了我对迁移的贡献。请正确缩进代码。我打印了周的值,现在打印一次的奇数周数打印了两次。您正在为(;;)增加中的循环变量
station和into-the-loop。你觉得这样对吗?把它改成了一个答案,这样我就可以更详细了。@EarlFuller:我为你做了缩进。代码是为人类编写的。当你要求我们阅读它时,双重/三倍/十倍。谢谢你注意到这一点。我真的希望这几周都能以同样的方式打印,但不要使用week数字在底部分开。例如,“第0周,A屋的蟑螂总数为4。第0周,B屋的蟑螂总数为5。”你认为我应该怎么做?=)你认为我应该改变函数定义吗?我太迷茫了。谢谢!@Earl我添加了可能的实现。事实上,我刚刚注意到我发布了第二个选项而不是第一个。让我来解决这个问题!对不起!=)选项1返回int
,所以如果你使用了它,你一定是复制错误了。小心注意,使用该选项时,您需要具有,例如,houseA+=roachesMigration(houseB,houseA);houseB-=roachesMigration(houseB,houseA)
在main
中。对于第二个选项,不赋值,函数不返回值,只需调用它。函数好的,丹尼尔,现在事情变得更有意义了。这是我的输出:对于第0周,第0周a屋的蟑螂总数是94,第1周B屋的蟑螂总数是85,蟑螂总数是85第一周,A屋的蟑螂总数是11只,第二周,B屋的蟑螂总数是2只,第二周,A屋的蟑螂总数是14只,B屋的蟑螂总数是2只,但我不知道为什么在第一周,两栋房子都被消灭了,而第二周,B屋的蟑螂总数保持不变。应该进行消灭仅在第4周或第8周。感谢您的帮助!我注意到我的week变量有问题。如果我正确地跟踪您的代码更改,此时您可能希望查看您的决定(如果)以了解何时发生灭绝,以及“周”是什么值是当你到达那里时。你可能需要调整你的决策计算。我希望灭绝发生在第4周和第8周,可以用mod运算符表示。你认为我应该改变什么?我的教授不希望我使用(if(week==4)| |(week==8)),出于某种奇怪的原因。Mod是一种非常好的方法。想想循环的第一次迭代。周是0,对吗?0%{anything}是0。因此,你在第1周就要灭绝。所以,考虑将迭代器变量调整为1到12,或者将Mod调整为(周+1)%4.如果你打算做很多编程,习惯于从0开始循环。至于你的教授不希望你做(周==4)| |(周==8),如果他/她将作业改为10年而不是11周,你会怎么做?如果要模拟的周数