gpt4 book ai didi

c++ - 涉及函数的C++程序...请帮助我

转载 作者:行者123 更新时间:2023-11-30 00:57:16 26 4
gpt4 key购买 nike

我很难处理两个函数。以下是项目说明:

作业:编写一个程序,在数周内跟踪两个相邻房屋中的蟑螂数量。房屋中蟑螂的数量将由以下因素决定:

  1. 每个房子的蟑螂初始数量是 10 到 100 之间的随机数。
  2. 每周,蟑螂的数量都会增加 30%。
  3. 这两所房子共用一堵墙,蟑螂可能会通过这堵墙从一所房子迁移到另一所房子。在给定的一周内,如果一所房子的蟑螂多于另一所,则蟑螂会从人口较多的房子迁移到人口较少的房子。具体来说,30% 的人口差异(向下舍入)会迁移。
  4. 灭虫人员每四个星期拜访其中一所房子,结果该房子里的蟑螂数量减少了 90%(四舍五入)。

这是我的代码:

#include <iostream>
#include <cmath>
using namespace std;

int house, increase, roaches, moreRoaches, fewerRoaches, filthyBeasts, change; // My variables for my four functions
int initialCount(int house);
int weeklyIncrease(int increase);
double roachesMigration(int moreRoaches, int fewerRoaches, int change);
int exterminationTime (int filthyBeasts);
// My four function prototypes

int main()
{
int houseA, houseB;

houseA = initialCount(houseA); //Initializing the initial count of House A.
houseB = initialCount(houseB); //Initializing the initial 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
{
roachesMigration(moreRoaches, fewerRoaches, change);
}
else if((houseB > houseA)) // Migration option 2
{
roachesMigration(moreRoaches, fewerRoaches, change);
}


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;
}

double roachesMigration(int moreRoaches, int fewerRoaches, int change)
{
more -= change;
fewer += change;
change = ((more - fewer) * .3);
return change;
}


int exterminationTime(int filthyBeasts) // Getting rid of the filthy little beasts!
{
filthyBeasts = (filthyBeasts * .1);
return filthyBeasts;
}

问题在于迁移和灭绝功能。我的代码运行良好,但在第 4 周和第 8 周,随机选择的房子应该被消灭,那所房子里的蟑螂数量应该比前一周减少 90%。你们认为我应该怎么做才能纠正这些问题?我真的需要我能得到的所有帮助!

最佳答案

关于这一行:

roachesMigration(change);

change 未在您的 main 函数中声明,因此出现错误。此外,roachesMigration 函数需要 3 个参数,而不是 1 个。

关于c++ - 涉及函数的C++程序...请帮助我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539943/

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