2011-12-15 11 views
2

私はこのプログラムをほとんど完了していますが、何かが正しくないのは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:

  1. The initial count of roaches for each house is a random number between 10 and 100.
  2. Each week, the number of roaches increases by 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.
  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.

ここに私のコードは次のとおりです。

#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

コードを正しくインデントしてください。 –

+0

私は週の値を印刷し、一度印刷された奇数週番号が今度は2回印刷されるようになりました。 –

+0

ループ変数を 'for(;;)'ステートメントとループ内でインクリメントしています。それはあなたに見えますか? – CanSpice

答えて

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 

しかし、私が印刷されている数週間は、彼らがどうあるべきかではないと思われます。

+0

ありがとうございます。私は実際に週を同じ方法で印刷することを望んでいますが、週番号は最下部で分けていません。たとえば、「0週目は、ハウスAのゴキブリ数は4です。0週目のハウスBのゴキブリ数は5です。私は何をすべきだと思いますか? =) –

1

また、roachesMigrationも間違っています。それは両方の家の中のゴキブリの数を変えなければならないが、ゴキブリの総数は変えてはならない。それはトータルを変えますが、家のうちの1つのゴキブリの数だけを変更します。両方のゴキブリ数を変更するには

は、あなたが

  1. は、関数計算に移行ゴキブリの数だけを持つことができ、メインループ
  2. に加算/減算通過を必要とする、機能変更に両方の値を持っていますその最初のオプションについては

参照またはポインタとして(私はここでの参照をお勧めします):

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

参照やポインタはまだコースでカバーされていない場合は、最初のオプションを持って行きます。

+0

関数定義で何を変更する必要があると思いますか?私はとても失われています。ありがとう! –

+0

@Earl可能な実装を追加しました。 –

+0

実際、最初のオプションの代わりに2番目のオプションを投稿したことに気がつきました。私はこれを修正しましょう!ごめんなさい! =) –

1

"week"という変数を使って何をしているのかよく見てください。特に、それは価値を変えています。

編集:ダニエルは入力中に答えを広げた(または完全に読まなかった)ので、マイグレーションへの寄稿を削除しました。

+0

私を助けていただきありがとうございます!私は週変数に問題があることに気付きます。 –

+0

あなたのコードが正しく変更されている場合は、この時点で、駆除が行われたときの決定と、そこに到達したときの「週」の値を調べることをお勧めします。決定の計算を調整する必要があるかもしれません。 –

+0

私は4週目と8週目に駆除を行いたいですが、これはmod演算子で表現できます。私は何を変えなければならないと思いますか?私の教授は、何らかの奇妙な理由のために(if(week == 4)||(week == 8))を使いたくない。 –

関連する問題