Sunday, August 2, 2009

The game of life in C++ code question?

Can someone look at this code and tell me what it is supposed to do?


for(row=row_index-1; row%26lt;=row_index+1; row++)


for(col=col_index-1; col%26lt;=col_index+1; col++)





This was the code that was provided to find the number of neighbors of each cell.

The game of life in C++ code question?
A For statement is one that executes as long as the conditional statement is true.





The first part (row=row_index-1) is the starting condition. This is where your program is at when the for loop begins.





The second part (row %26lt;= row_index+1) is the conditional statement. As long as this statement remains true, the for loop will continue to execute.





The third part (row++) is the "updater" to the equation. Basically, it increases the row_index by 1, so the For statement reruns, this time testing the updated value against the conditional statement.
Reply:This code walks through each neighboring element of the array surrounding the element at (row_index, col_index).


If there is a third line here it would would probably be a comparison against the contents of the array position and a decision about the contents.


By the way some where you have to test for a boundary because the row or column in these loops could be outside the the array and you would have a bug either a crash or an unpredictable condition.

plum

No comments:

Post a Comment