We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# 2D Array Search

on
Medium Priority
248 Views
Hi!

Working on what to everyone else is a simple program but to me is proving quite difficult.  Program simulates birth and death within a two dimensional array.

Specifically, after the user specifies that life exists in say seven elements, successive generations are displayed using these two rules:

1) an organism in a cell survives to the next generation if exactly 2 or 3 of its neighbors are living, otherwise it dies

2) an organism is born into an unoccupied cell if exactly 3 of its neighbors are unoccupied.

Obviously I'm a student and I don't even know where to begin writing what is probably a series of loops to search for values in the six surrounding elements.

Would someone please show me what this code would look like and explain the why of it?

Thanks much.

Keith Connor

Comment
Watch Question

## View Solution Only

Commented:
For any current cell, located at position i, j, the following
loops count the number of living and dead cells:

nof_alive= 0;

for (di= -1; di <= 1; di++)
for (dj= -1; dj <= 1; dj++)
else
nof_alive++;

Now determine what to do with the current cell:

if (cell[i][j] == alive) {
if (nof_alive == 2 || nof_alive == 3)
next_cell[i[[j]= alive;
else
}
else if (nof_alive == 3)
next_cell[i][j]= alive;
else

The array nex_cell contains the next generation of
all the cells ...

does this clarify things a bit?

kind regards,

Jos aka jos@and.nl

ps. Note that some care has to be taken for 'wrap around',
i.e. the cells next to the border of the array must be taken
care of in a sensible way. The normal approach is to
define the cell array as a torus, i.e. the rightmost cells
have the leftmost cells as their right neighbors etc.

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Jos,

Thanks!  Of course being a student I'm going to have to 'digest'
this :).  Also, for clarification this will only provide the results of one cell or all?

Keith Connor

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile