Tuesday, July 28, 2009

C++ explaination?

Can someone explain what does return and state update do in c++?





state update(int n,state now)


{


if((n%26gt;3)||(n%26lt;2))


return(dead);


if(n==3)


return(alive);


if((n==2)%26amp;%26amp;(now==alive))


return(alive);


return(dead);


}





I will give 10 points for those who can explain that to me

C++ explaination?
The update function will return a state value that is either dead or alive.





It requires 2 bits of information first - what is it now and an integer n, which has some meaning internal to the program.





If n is greater than 3 or less than 2 then it returns dead.


If n is 3 it returns alive.


If n is 2 and currently alive it returns alive.


Otherwise it returns dead.
Reply:This look like the game of Life. Not the board game, but Conway's game of life. It is a simple population simulation. There is a grid of cells with a starting set of cells tagged as alive. In each iteration, a cell becomes alive or dead based on the number of neighbors.





The function you list looks like the calculation for alive and dead. The post above describes the behavior well.


If the cell has 4-7 neighbors or 0-1, it dies.


If the cell has exactly 3 neighbors it becomes alive.


If the cell has exactly 2 neighbors, it stays them same.


In any case not taken care of, the cell is dead.
Reply:You're dead if you are not between (approx) 2.00001 and 3.000001


No comments:

Post a Comment