Question: How would this be implemented in C? generation computation Conway's Game of Life. Because each generation of the Game of Life is dependent only on

How would this be implemented in C? generation computation

Conway's Game of Life. Because each generation of the Game of Life is dependent only on the previous generation, it is convenient and efficient to perform all computations on two identical matrices. One matrix represents the current generation, and the other represents the next. After each generation, the roles of the two generations will swap. This can be conveniently accomplished by declaring a three-dimensional matrix, where the third dimension is 2, as follows:

char grids [2][ Y ][ X ];

This represents two matrices with an outer Y dimension of X and an inner X dimension of Y. In generation 0, grids[0] would be used as the current generation, and the next generation computed on grids[1]. In generation 1, their roles would swap. The roles can be easily swapped using the modulus operator, %.

For example:

current = 0;

for (int current = 0; ; current = ( current + 1) % 2) {

/* Do something */ }

Note that the calculation before the modulus operator must be parenthesized. The value of current will alternate between 0 and 1 in every iteration of this loop.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!