1

I am having a hard time with two functions. 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, moreRoaches, fewerRoaches, filthyBeasts, change; // My variables for my four functions
int initialCount(int house);
int weeklyIncrease(int increase);
double roachesMigration(int moreRoaches, int fewerRoaches, int change);
int exterminationTime (int filthyBeasts);
// My four function prototypes 

int main()
{
    int houseA, houseB;

    houseA = initialCount(houseA); //Initializing the initial count of House A.
    houseB = initialCount(houseB); //Initializing the initial 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
        {
            roachesMigration(moreRoaches, fewerRoaches, change);
        }
        else if((houseB > houseA)) // Migration option 2
        {
            roachesMigration(moreRoaches, fewerRoaches, change);
        }


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

double roachesMigration(int moreRoaches, int fewerRoaches, int change)
{
    more -= change;
    fewer += change;   
    change = ((more - fewer) * .3);
    return change; 
}


int exterminationTime(int filthyBeasts) // Getting rid of the filthy little beasts!
{
    filthyBeasts = (filthyBeasts * .1);
    return filthyBeasts;
}

The issues are with the migration and extermination functions. My code runs fine, but 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. What do you guys think I should do to correct these issues? I really need all the help I can get!

4 Answers4

2

Regarding this line:

roachesMigration(change);

change is not declared in your main function, hence the error. Also, roachesMigration function expects 3 parameters and not 1.

Igor
  • 26,650
  • 27
  • 89
  • 114
  • Igor, the real problem is I don't know what to call in main for the migration function. I tried setting each house equal to the migration function, but that fails as well. What should I call in main? – Earl Fuller Dec 16 '11 at 20:54
  • I think you should read more good books about programming, and focus instead of having a small program running correctly (and solving a small subset of your question). So start with a tiny program, make it run correctly (learn how to use the debugger), and enhance & improve it. Repeat that process till you solved your homework. – Basile Starynkevitch Dec 16 '11 at 21:11
  • Thank you, Basile. I'm new to C++ and am so confused. I posted my code that is running now. Can you please take a look at it for me and tell me how to make the extermination function work? I would really appreciate it. I'm using a good book to help me. – Earl Fuller Dec 16 '11 at 21:17
2

The variable change is not a global variable, but appears inside main (so it has no meaning inside main).

Your roachesMigration fonction is declared with three formal arguments (without default values), but you use it with one actual argument.

Ask your compiler to give you all the warnings and to produce debugging information (g++ -Wall -g on Linux). Improve the code till you get no warnings.

Learn to use a debugger (e.g. gdb on Linux).

Have fun.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Depending on the instructor, you will get zero marks for this code, even if you can get it to work perfectly! This is because you have not used any object orientated design in building your code. In C++, that means classes.

What sort of object do you need for this problem. A house!

What sort of attribute should your house have? Roaches!

So something like this:

class cHouse
{

 int MyRoachCount;

 ...

};

If you start fresh, like this, you will find things start to fall neatly into place.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • this program was assigned long before we learned about classes, so I don't think my professor wanted me to use classes to do it. – Earl Fuller Dec 16 '11 at 21:25
  • But you know about classes now, right. You know that they make designing and writing code very much easier? So, you probably should use them ... unless you think there is a job out there for a programmer who cannot use classes. – ravenspoint Dec 16 '11 at 21:30
  • Okay. I'll use classes for this project since they'll make it easier. Thank you. – Earl Fuller Dec 16 '11 at 21:34
  • Now you're talking like a coder! If you get stuck, ask again. – ravenspoint Dec 16 '11 at 21:36
1

One possible way to handle the migration is like this pseudocode:

// compute size of migration
count = migration(houseA, houseB)

if (houseA < houseB)
    add count to houseA
    subtract count from houseB
else
    add count to houseB
    subtract count from houseA
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70