Download the following files for the first program assignment:
Look at the constructor for the Environmnet class and determine how fish are created in the environment. Use the file fishmatrix.dat to create an 20 x 20 environment with 25 fish in it.
Use fishmatix.cpp as the driver program to look at the environment 100 times, and each time every fish has a 2% probability of dying. If the fish dies, remove it from the environmnet. Do not use the Environment::AllFish function, but instead loop through all possible positions yourself looking for fish. You will need to understand how the IsEmpty function works and what a Position class is. To kill a fish, create an environment function called KillFish(position) that kills the fish that is in fish's position.
In order to kill a fish you will have to look in the fish class and understand what makes a fish alive and what makes a fish dead. Look at the fish's states.
FishMatrix.cpp displays the matrix before and after looking at the environment 100 times. You also need print out how many fish are left in the environment. Look at the states of the environment class, and add another environment function that will return the number of fish left in the environmnet called FishCount.
typedef apmatrix<int> ResultsType;
The diagram below shows an example of the array results for NumCandidates = 4 and NumDistricts = 6. (The code you will write should work with any positive values of NumCandidates and NumDistricts. Beware that the matrix is labeled 1 - N, whereas C++ starts indexing at 0.)
DISTRICT C A 1 2 3 4 5 6 N 1 0 5 0 0 2 1 D 2 20 30 10 5 5 0 I 3 1 1 1 1 1 1 D 4 0 1 0 0 0 0 A T E
For example, for the given array results shown above, the expression NumVotes(results, 1) should evaluate to 8 since Candidate 1 received a total of 8 votes; the expression NumVotes(results, 3) should evaluate to 6 since Candidate 3 received 6 votes.
Write the function NumVotes whose header is below.
int NumVotes(const ResultsType & results, int c) // precondition: 1 <= C <= NumCandidates
For example, given the array results shown above, the expression Winner(results) should evaluate to 2 since Candidate 1 received 8 votes, Candidate 2 received 70 votes, Candidate 3 received 6 votes, and Candidate 4 received 1 vote, with Candidate 2 receiving the most votes.
Write the function Winner whose header is the following.
int Winner(const ResultsType & results) // postcondition: Winner = C, where NumVotes(results, C) > NumVotes(results, i) // for all i such that i != C and 1 ¾ i ¾ NumCandidates
Oil moving into squares beyond the bounds of the array is lost to the simulation. At each time step, the program goes through the entire array and calculates the amount of oil in each square based on the amounts in the neighboring squares at the previous time step. To do this correctly you need two matricies -- one to hold all the old values while the new values are computed, and one to hold the new values. The state of the simulation at any time step is shown by displaying the contents of the array on the screen. To keep the display manageable, we assume that the maximum amount of oil in a square, measured in barrels, is 999, and only the digits to the left of the decimal point are displayed. Therefore, each square in the grid takes up 3 places plus a space in the display, and so the maximum array size that will fit on the screen is 20 columns by 25 rows. If there is a partial barrel (less than 1 barrel) in a square output a "*" in the grid location. Since it would be impractical to generate this display at each step, the program runs for a given number of time steps and then displays the array. Initially (at time step zero), a given amount of oil is spilled into the middle square in the western-most column (middle of left edge of display), and all other squares are initialized to zero. The program inputs the following at the start of the run:
The program runs for the given number of steps and displays the resulting matrix. (There is no need to display the initial configuration.) The program should ask if you want to continue with the simulation. If so, input the number of time steps to run again (continuing where the simulation left off previously), and again display the results. In this way you can continue as long as desired.
Each time display stops include the ocean grid along with the following information:
Your output should look the following:
Enter number of barrels of oil: 10 Enter the evaporation rate: 0 Enter the wind velocity: 10 Enter number of time steps to compute: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 * 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 * 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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