WEEK 20 RESULTS
Fish With Weight 20
Plant weight 13.95
Plant weight 21.05
Plant weight 45.19
Plant weight 19.74
Plant weight 29.69
Plant weight 46.15
FISH DIED
Plant weight 9.49
Fish With Weight 6
Plant weight 28
15Plant weight 42.55
Fish With Weight 3
Ending simulation
FISH DIED
PLANT DIED
PLANT DIED
PLANT DIED
PLANT DIED
PLANT DIED
PLANT DIED
PLANT DIED
FISH DIED
PLANT DIED
PLANT DIED
FISH DIED
instead, on mine it looks like:
WEEK 20 RESULTS
Fish With Weight 1
Plant weight 1
Plant weight 1
Plant weight 1
Plant weight 1
Plant weight 1
Plant weight 1
Plant weight 1
Fish With Weight 1
Plant weight 1
Plant weight 1
Fish With Weight 1
Ending simulation
PLease help me figure out why the output is different.
The int main() is written for you, however you will need to implement the following functions
• void buildPondSimulator(ifstream& infile, organism *** pond) - this function takes in an already opened filestream that reads from a CSV file and your empty ”2D” array of organism objects, your job will be to insert an herbivore or plant object into pond[r][c]. Every line in the CSV file will contain
ORGANISM_TYPE , SIZE , GROWTH_RATE , X_COORDINATE , Y_COORDINATE
Once you parse the line and convert each element into its correct type (SIZE and GROWTH_RATE should be converted in double and X_COORDINATE and Y_COORDINATE should be converted into int, and ORGANISM_TYPE will remain a string), you then assign pond[X_COORDINATE][Y_COORDINATE] with new herbivore(size, rate, size * 0.1) or new plant(size, rate) based on whether ORGANISM_TYPE contains "FISH" or "PLANT" respectively.
You may use getNextField function from previous assignments or you can use a stringstream type to parse each comma separated line. You can see a description of that in the video (it’s linked at the end of the pdf).
Not all elements of pond[r][c] will be assigned to point to an object, thus for those elements assign pond[r][c] = NULL or pond[r][c] = nullptr
• void simulateAWeek(organism *** pond, randNum& rN) - this function simulates one week of the pond simulator, you will simulate 100 activities and each activity does the following
1. Get a random x1, y1, x2, and y2 indices to investigate two potential organisms in the pond array
2. Create two pointers organism * o1, * o2 and set them with the content in pond[x1][y1] and
pond[x2][y2] respectively
3. Check the contents in o1 and o2 for whether they are NULL or if they point to an herbivore
and/or plant object (this could involve using a dynamic_cast)
4. If they are both NULL then this is a failed activity, thus nothing happens
5. If they both point to plant objects, then this would be a failed activity, thus nothing happens
6. If one of the pointers is NULL and the other is a plant then nothing happens
7. If one of the pointers is NULL and the other points to an herbivore, assign NULL to where the
herbivore object used to be be and assign the herbivore obect to where the NULL used to be
8. If they both point to herbivore objects then you need to swap their positions in the pond array
9. If one of the pointers points to an herbivore object and the other points to a plant object,
then you must called the herbivore object’s nibble function and pass in the plant object as the
parameter (you may need to dereference, use the arrow operator, and/or use dynamic_cast here), then you swap their positions in the pond array (I guess the plant submerges underground and
spawns where the fish used to be)
Once all 100 activities are simulated, you call simulateWeek() function for each element. Check each object if it is still alive, if no longer alive then deallocate the object pond[r][c] and then set pond[r][c] to NULL or nullptr
Then you call the outputOrganism function for each object in the pond.
You haven't provided enough of your code to answer this.
Also, it helps if your use the Code format tags (in the palette to the right) to format your code. Also the code stops in the middle of a commented out section.
It's also a little odd to be asked to use C++ in a simulation that'll generate data without using STL containers, just new/delete on stuff.
This is what happens when you use raw pointers, you handle the heap incorrectly.
1 2 3 4 5 6 7 8 9 10
void clearSimulation(organism*** pond) {
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
delete [] pond[i][j];
for (int i = 0; i < ROWS; i++)
delete [] pond[i];
delete [] pond; // you only had this
}
and this is a serious logical error in addition to the memory thing