我差不多完成了这个程序,但main
中的某些内容并不正确。以下是项目说明:
分配:
编写一个程序,记录两个相邻房屋中的蟑螂数量,持续数周。房屋中蟑螂的数量将由以下因素确定:
- 每个房屋的初始蟑螂数是10到100之间的随机数。
- 每周,蟑螂的数量增加30%。
- 这两座房子共用一堵墙,蟑螂可以通过这条墙从一条墙迁移到另一条墙。在一个星期内,如果一个房子的蟑螂比另一个房子多,那么人口较多的房子里的蟑螂会迁移到人口较少的房子。具体而言,人口中30%的差异(向下舍入)会迁移。
- 每四个星期,其中一个房屋被一个灭虫者访问,导致该房屋内蟑螂数量减少90%(四舍五入)。
醇>
这是我的代码:
#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;
}
迁移和清除功能存在问题。我收到编译器的错误消息,上面写着“错误:语义问题:没有匹配函数来调用'roachesMigration'”。此外,在第4周和第8周,随机选择的房屋应该被消灭,并且该房屋中的蟑螂数量应比前一周减少90%。你认为我应该怎么做才能纠正这个问题?我非常感谢所有的帮助!
答案 0 :(得分:3)
提示:我注意到你在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
但是,我怀疑打印的周数不是应该的。
答案 1 :(得分:1)
此外,您的roachesMigration
错了。它应该改变两个房子里的蟑螂数量,但不能改变蟑螂的总数。它改变了总数,但只改变了其中一个房屋中的蟑螂数量。
要更改两个蟑螂计数,您可以
第一个选项:
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;
}
如果课程中尚未涵盖参考和指针,请使用第一个选项。
答案 2 :(得分:1)
仔细查看您对变量“week”所做的事情。特别是,它正在改变价值。
编辑:Daniel在我打字时扩展了他的答案(或者我没有完全阅读),所以我删除了我对迁移的贡献。