COMP 104 : Programming

Lab 6 : Parasite Simulation


In this lab, you are going to simulate a forest plagued by a parasite.

A parasite plagues a forest by infecting the trees and causing
them to defoliate (lose their leaves). Defoliated trees are removed from the forest and new trees are planted in their places, which in turn may become infected by the parasite. The following three rules define a simple model of the forest environment:

1. An infected area becomes defoliated the following year;
2. A defoliated area becomes green the following year;
3. An infected area infects its neighbors to the north, south, east and west the following year, if they are currently green.

A biologist requires a computer program that will use the rules given above to simulate the
changes in the forest over a number of generations (one generation = one year).
The program should represent the state of the forest by a grid of 25 letters. For example, the state

GGGGG
GGIGG
GGDDG
GGGGG
GGGGG

indicates that most of the forest is green (G), with
only one infected area (I) and two defoliated areas (D).
The user of the program should be able to specify the initial state of the forest, and to enter the number of generations for which the simulation will run.

There are two parts for this lab.

Part 1. Design and implement necessary functions for the following
main() function to run properly:

void main() {
  char stateOfForest[5][5]; // the state of the forest array
  int generation; // the number of generations to simulate the forest

  get_initial_state(stateOfForest);
        // get the initial state of the forest
        // from the user, and save it in stateOfForest
  get_generation(generation);
        // get the number of generations to simulate from the user
  for(int i=1; i<= generation; i++) {
    print_state(stateOfForest); cout << endl;
    //print out the state at each generation - good for debugging your program
    update_state(stateOfForest);  //update the state using the three rules above
  }
  print_state(stateOfForest);
        // print out the final state
}

Part 2. One problem with the simulation program above is that it runs
for a fixed number of generations regardless whether the state of
the forest is stable or not. For instance, if the initial state of
the forest is all green, then it stays that way for ever. Modify
the above simulation program so that it stops as soon as the state
becomes stable.

You should submit the following files for evaluation:

1. YourFirstName_YourLastName_Comp104_Lab6_Part1.cpp

This file should contain only the main() function above, plus
appropriate #include and using directives. In particular, it should
include your header file for the lab (see below).

2. YourFirstName_YourLastName_Comp104_Lab6_Part2.cpp

This file should contain your modified main() function as required
in part 2 of the lab.

3. YourFirstName_YourLastName_Comp104_Lab6_Forest.h

This header file should contain the prototypes of all the functions that
you introduce and use in your C++ source codes for the lab, that is,
those functions that are not in the standard library but needed
in order to run your programs.

4. YourFirstName_YourLastName_Comp104_Lab6_Forest.cpp

This file should contain the implementations of the functions in
the above header file.

For example, if your name is "Cindy Li", then you need to submit
the following four files:
Cindy_Li_Comp104_Lab6_Part1.cpp, Cindy_Li_Comp104_Lab6_Part2.cpp,
Cindy_Li_Comp104_Lab6_Forest.h, Cindy_Li_Comp104_Lab6_Forest.cpp.


Hints: This is not a hard problem, but it is easy to get a messy
program if you are not careful. So don't rush into writing the codes,
think about how to solve the problem in the cleanest possible way.
To see the difference between Part 1 and Part 2, try the following
initial state, and simulate it for 30 generations:

GGIGD
GGGGG
GGGGG
GGGGG
GGGGG



Demo your working program for your TA. The TA will check your source code to make sure you made reasonable use of arrays and functions. If for some reason you cannot finish before the end of the lab period, email your program to your TA by Sunday 30 October 5PM (please include your name and lab section as a comment on line 1 of your program).