I am having trouble with the random number generator. For week 0 only, I want to set the starting number of roaches in House A (totalRoachesInHouseA
) to 97 and the number of roaches in House B (totalRoachesInHouseB
) to 79. Then, for weeks 1 to 10, I want to generate random numbers for both houses. I am trying to get the same numbers in the example simulation below.
Initial Population: Week 0: House 1 = 97; House 2 = 79
Week 1: House 1 = 119; House 2 = 109
Week 2: House 1 = 151; House 2 = 144
Week 3: House 1 = 194; House 2 = 189
Week 4: House 1 = 25; House 2 = 247
Week 5: House 1 = 118; House 2 = 235
Week 6: House 1 = 198; House 2 = 260
Week 7: House 1 = 280; House 2 = 314
Week 8: House 1 = 37; House 2 = 395
Week 9: House 1 = 187; House 2 = 374
Week 10: House 1 = 315; House 2 = 414
Here are the instructions for the project:
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. Your implementation must use functions and local variables.
Here's what I have so far:
int roachesInHouseA, roachesInHouseB; //my two houses.
int temporaryHouseA, temporaryHouseB;
double totalRoachesInHouseA, totalRoachesInHouseB; //the total count of houses.
int week;
int main( )
{
int temporaryHouseA = 0; // storage for arithmetic
int temporaryHouseB = 0; // storage for arithmetic
int totalRoachesInHouseA = 97; // test number for A
int totalRoachesInHouseB = 79; // test number for B
roachesInHouseA = rand() % 100 + 10;
roachesInHouseB = rand() % 100 + 10;
//The functions declaring the random count of roaches in both houses between
//10 and 100.
for (int Z = 1; Z < 12; Z++) // My for loop iterating up to 11 weeks.
{
totalRoachesInHouseA = totalRoachesInHouseA + roachesInHouseA * .3;
totalRoachesInHouseB = totalRoachesInHouseB + roachesInHouseB * .3;
// My function declaring that the roach population explodes by 30% weekly.
cout << "The number of roaches in House A is " << totalRoachesInHouseA << endl;
cout << "The number of roaches in House B is " << totalRoachesInHouseB << endl;
if ((week == 4) || (week == 8)) // It's extermination time!
{
totalRoachesInHouseA = totalRoachesInHouseA - roachesInHouseA * .9;
totalRoachesInHouseB = totalRoachesInHouseB - roachesInHouseB * .9;
}
else if (totalRoachesInHouseA > totalRoachesInHouseB) // Migration from
//House A to HouseB
{
temporaryHouseA = totalRoachesInHouseA * .3;
totalRoachesInHouseA = totalRoachesInHouseA - temporaryHouseA;
totalRoachesInHouseB = totalRoachesInHouseB + temporaryHouseA;
}
else if (totalRoachesInHouseA < totalRoachesInHouseB) // Migration from
// House B to House A
{
temporaryHouseB = totalRoachesInHouseB * .3;
totalRoachesInHouseB = totalRoachesInHouseB - temporaryHouseB;
totalRoachesInHouseA = totalRoachesInHouseA + temporaryHouseB;
}
}
}