简体   繁体   English

C ++ Roaches项目…消除功能有缺陷

[英]C++ Roaches project… Extermination function is flawed

I'm almost done with this program, but something's not right in main . 我几乎已经完成了该程序,但是main有些main Here are the project instructions: 这是项目说明:

Assignment: 分配:
Write a program which keeps track of the number of roaches in two adjacent houses for a number of weeks. 编写一个程序,在两个星期内跟踪两个相邻房屋中的蟑螂数量。 The count of the roaches in the houses will be determined by the following: 房屋中蟑螂的数量将由以下因素决定:

  1. The initial count of roaches for each house is a random number between 10 and 100. 每个房屋的蟑螂初始数量是10到100之间的随机数。
  2. Each week, the number of roaches increases by 30%. 每周,蟑螂的数量增加30%。
  3. The two houses share a wall, through which the roaches may migrate from one to the other. 这两所房子共用一堵墙,蟑螂可以通过墙从一个墙迁移到另一个墙。 In a given week, if one house has more roaches than the other, roaches from the house with the higher population migrate to the house with the lower population. 在给定的一周内,如果一间屋子的蟑螂比另一间屋子的蟑螂多,则蟑螂会从人口较多的屋子迁移到人口较少的屋子。 Specifically, 30% of the difference (rounded down) in population migrates. 具体来说,人口差异的30%(向下舍入)迁移。
  4. Every four weeks, one of the houses is visited by an exterminator, resulting in a 90% reduction (rounded down) in the number of roaches in that house. 每四周,除虫器会拜访其中一间房屋,从而使该房屋中蟑螂的数量减少90%(向下舍入)。

Here's my code: 这是我的代码:

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

There are issues with the migration and extermination functions. 迁移和清除功能存在问题。 I'm receiving an error message from the compiler that says, "error: Semantic Issue: No matching function for call to 'roachesMigration'". 我从编译器收到一条错误消息,内容为“错误:语义问题:没有匹配函数可调用'roachesMigration'”。 Also, at weeks 4 and 8, the randomly selected house should get exterminated, and the number of roaches in that house should be 90% less than the previous week. 另外,在第4周和第8周,随机选择的房屋应灭绝,该房屋中的蟑螂数量应比前一周减少90%。 What do you guys think I should do to correct this issues? 你们认为我应该怎么做才能解决此问题? I really appreciate all help! 我真的很感谢所有帮助!

Hint: I notice that you're printing the value of week out at the end of your for loop. 提示:我注意到您正在for循环的结尾打印出week的值。 In other words, your output should be something like: 换句话说,您的输出应类似于:

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

However, I suspect that the weeks being printed are not what they should be. 但是,我怀疑所打印的周数不是应该的。

Also, your roachesMigration is wrong. 另外,您的roachesMigration错误。 It should change the number of roaches in both houses, but not the total number of roaches. 它应该更改两个房屋中蟑螂的数量,但不能更改蟑螂的总数。 It changes the total, but only the number of roaches in one of the houses. 它改变总数,但只改变其中一间房屋的蟑螂数量。

To change both roach counts, you can 要更改两个蟑螂计数,您可以

  1. have the function calculate only the number of roaches that migrate and add/subtract in the main loop 具有仅计算在主循环中迁移和添加/减去的蟑螂数量的功能
  2. have the function change both values, which requires passing them as references or pointers (I'd recommend references here) 让函数同时更改两个值,这需要将它们作为引用或指针传递(我建议在这里推荐引用)

For the first option: 对于第一种选择:

int roachesMigration(int more, int fewer)
{
    return ((more - fewer) * 3) / 10;
}

And for the second: 第二:

void roachesMigration(int & more, int & fewer)
{
    int migration = ((more - fewer) * 3) / 10;
    more -= migration;
    fewer += migration;
}

If references and pointers have not yet been covered in the course, go with the first option. 如果本课程尚未涵盖引用和指针,请选择第一个选项。

Look carefully at what you're doing with the variable "week". 仔细查看变量“ week”的作用。 Especially, where it is changing value. 特别是在改变价值的地方。

Edit: Daniel expanded his answer while I was typing (or I didn't read it completely), so I removed my contribution to the migration. 编辑:丹尼尔(Daniel)在我键入内容时扩大了答案(或者我没有完全读懂它),所以我删除了对迁移的贡献。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM