私はこのプログラムをほとんど完了していますが、何かが正しくないのはmain
です。ここでは、プロジェクトの命令です:C++ Roachesプロジェクト...駆除機能に欠陥があります
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:
- The initial count of roaches for each house is a random number between 10 and 100.
- Each week, the number of roaches increases by 30%.
- 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.
- 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.
ここに私のコードは次のとおりです。
#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%少なくなるはずです。あなたはこの問題を解決するために何をすべきだと思いますか?私は本当にすべての助けに感謝!
コードを正しくインデントしてください。 –
私は週の値を印刷し、一度印刷された奇数週番号が今度は2回印刷されるようになりました。 –
ループ変数を 'for(;;)'ステートメントとループ内でインクリメントしています。それはあなたに見えますか? – CanSpice