Solved

2D Array Search

Posted on 1997-07-08
2
224 Views
Last Modified: 2010-04-10
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

0
Comment
Question by:connork
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
jos010697 earned 100 total points
ID: 1164386
For any current cell, located at position i, j, the following
loops count the number of living and dead cells:

nof_dead= 0;
nof_alive= 0;

for (di= -1; di <= 1; di++)
   for (dj= -1; dj <= 1; dj++)
      if (cell[i+di][j+dj] == dead)
         nof_dead++;
      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
      nex_cell[i][j]= dead;
}
else if (nof_alive == 3)
   next_cell[i][j]= alive;
else
   next_cell[i][j]= dead;

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.

0
 

Author Comment

by:connork
ID: 1164387
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
 


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 155
IdTCPClient1->Disconnect(); not working 3 92
c++ syntax question 9 66
What is sub-make ? 2 106
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question