Question
Write a time-stepped simulation program in C to model the population dynamics of a city. The city is represented as a checkerboard-like grid of size
Write a time-stepped simulation program in C to model the population dynamics of a city. The city is
represented as a checkerboard-like grid of size 50x50 (i.e., 2500 grid cells). Each cell represents a house.
There are two types of people in the city: red and green people. Each cell will be in one of three possible
states: (1) green if occupied by a green person, (2) red if occupied by a red person, or (3) empty if the cell
is unoccupied. The simulation operates according to the following rules:
1. The city initially contains 40% green households, 40% red households, and 20% unoccupied
households randomly distributed across the city.
2. Each cell has 8 neighboring cells. For cells on the left, right, top and bottom edges of the grid,
assume wrap around connections e.g., for a cell along the leftmost (westernmost) edge, its
neighbor to the west is the cell in the same row, but at the right edge of the grid. Same for cells
along the top/bottom edge.
3. A green person will be happy if that person has at least one green neighbor and at least 30% of
the neighboring cells that are occupied are also green (unoccupied cells are not counted in this
computation). Otherwise, the person will feel isolated and will be unhappy. Similarly, a red
person will be happy or unhappy using the same rules, but for red neighbors (see examples
below).
4. At each time step, a happy person will remain in the same cell that time step. An unhappy person
will move to another randomly selected unoccupied cell without considering the color of its
neighbors in the new cell. Hint: you may select a new cell by randomly selecting any cell in the
grid and if the selected cell is occupied, repeat this procedure until an unoccupied cell is selected.
An unhappy occupant always moves to a different, unoccupied cell, and the cell that person left
then becomes unoccupied. A cell vacated by an individual in time step T may be allocated to
someone seeking to move in time step T.
5. The simulation proceeds as follows. In each time step:
a. Scan through all occupied cells and determine if each occupant is happy or unhappy.
b. Make a second pass through all the cells. For each unhappy occupant, the occupant
moves to a new, randomly selected cell using the rule described above.
c. After the second pass is complete, advance to the next time step and repeat these steps.
To complete this assignment:
1. Write a function init that initializes the grid by placing the red and green individuals onto the
grid as discussed above.
2. Write a function print that prints the grid with an R indicating if a red person occupies a grid
cell, G if a green person occupies the cell, or a blank character if the cell is unoccupied (see
example below).
3. Write a function sim that simulates one time step of the simulation.
4. Write a main program that (1) prints the initial configuration of the board, (2) prints the state of
the board after 2 time steps, and (3) prints the state of the board after 10 time steps.
5. Briefly comment on the results produced by these simulation runs.
Example print out (5 by 5 grid):
GGRGR
RRRR
GG
GRGGG
GRR R
In the above configuration, the green cell labelled G is happy because 2 out of 4 occupied neighbor cells
(50%) are green, and 50% > 30%. The red cell labelled R is unhappy because only 1 out of 7 occupied
neighbor cells (considering the wrap around connections) or 14% are red, and 14% < 30%, so this person
will move to another cell for the next time step. Note empty cells do not count in computing happiness.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started