I'm almost done with this program, but something's not right in 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.
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.
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 roaches, int addedRoaches);
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.
{
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 (week % 4 == 0) // It's extermination time!
{
if ((rand() % 2) == 0) // Get a random number between 0 and 1.
{
houseB = exterminationTime(houseB);
cout << houseB << endl; // TEST ONLY
}
else
{
houseA = exterminationTime(houseA);
cout << houseA; // TEST ONLY
}
}
}
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 roaches, int addedRoaches) // The migration functions between the house with the greater amount of roaches
{
addedRoaches = ((roaches * .3) + addedRoaches); // and the house with the lesser amount of roaches --> a 30% transfer.
return addedRoaches;
}
int exterminationTime(int filthyBeasts) // Getting rid of the filthy little beasts!
{
filthyBeasts = (filthyBeasts * .1);
return filthyBeasts;
}
Everything compiles and runs, but the roaches are not being exterminated from the random house that is selected. At weeks 4 and 8, the number of roaches in the selected house should be 90% less than the previous week. What am I doing wrong?
[code]"Please use code tags"[/code]
¿Don't you see anything funny in the weeks that you output? if((houseA > houseB) && (week < 11) && (week++)) // Migration A to B ¿care to explain that condition?
Also your migration function is incorrect
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.
Roach are not magically created but they transfer. So both values (houseA and houseB) will change.
Okay, I see my errors now. I have removed the week++ incrementations in both if-else statements because I already have one in the for loop.
Do you think I should I create a total variable in the Migration function? What do you think I should do to transfer 30% of the total population of House A to House B?
I have actually changed my code and it compiles and runs fine, but I am still not getting the correct numbers for the migration and extermination functions. Can you please evaluate it for me and tell me what I should do to fix these issues? I really appreciate your help!
Here's my new code:
//
// main.cpp
// OverdueRoachesProject
//
// Created by Earl Fuller Jr. on 12/8/11.
// Copyright 2011 CUNY Hunter College. All rights reserved.
// Professor Wisniewska, I'm sorry this is so late! I've busted my brain for some weeks now
// and had to get some help in order to understand this program, but I understand it now!
// Thank you so much for helping me and being so patient with me!
#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 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.
{
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 (week % 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)
{
return ((more - fewer) * .3);
}
int exterminationTime(int filthyBeasts) // Getting rid of the filthy little beasts!
{
filthyBeasts = (filthyBeasts * .1);
return filthyBeasts;
}
//houseB = roachesMigration(houseA, houseB);
int roachesMigration(int more, int fewer)
{
return ((more - fewer) * .3);
}
Now you are calculating the ammount that moves. But you are not updating properly.
Again, the quantity in both houses will change
You need to do
1 2
Bigger -= change;
Lower += change;
You've got another mistake in extermination, you are rounding up the 90%
Also int initialCount(int house) you are not using that parameter. ¿why is it a parameter then? if((houseA > houseB) && (week < 11)) You are already checking week<11 in the loop condition
Okay, I have updated my code as you said. I took out the (week<11) condition inside the for loop. I inserted the bigger and lower code into my migration function, but I get an error that reads: "error: Semantic Issue: No matching function for call to 'roachesMigration'". What does that mean, and how do you think I should fix that?
Also, how do I fix the extermination mistake?
Here's my updated code now. Notice that I added a parameter for change, since the compiler didn't recognize it.
I really appreciate your helping me! =)
#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.
{
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 (week % 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)
{
return ((more - fewer) * .3);
more -= change;
fewer += change;
}
int exterminationTime(int filthyBeasts) // Getting rid of the filthy little beasts!
{
filthyBeasts = (filthyBeasts * .1);
return filthyBeasts;
}