Okay, every cellular automaton is built around a "recurrance relation", so that the state at time t depends on the state at time t-1. So every cellular automaton has a basic structure where you have some data structure, like an array, that represents the state. So, abstractly, your program will look like
State t
/* initialize t */
while(/* end condition isn't satisfied */){
t = rule(t);
/* display state somehow */
}
where rule(t)
is a function that computes that next state.
The next step is to come up with a data structure to represent the state. That's actually easy -- the state of an elementary 1 d cellular automaton is just a vector of 1s and 0s.
So you need an array of 14 small numbers. Space is not going to be an issue, so use int:
int t[14] ; /* state vector*/
The end condition is easy -- you're supposed to do 55 rows, so you need
int count = 0;
while(count++ < 55)
Notice that gives you rows 0-54, which is 55. A good basic pattern in C is that you start at 0, post-increment, and test less-than. Probably 9 out of 10 loops you write in C will have that pattern.
Now, finally, the question is how to implement your rule. Unfortunately, since this is C, you can't do it quite as simply as in my description; the rule has to update the vector in place. That's going to look a lot like
void rule(int t[]){
/* compute the update here */
}
Now, I'm not going to tell you exactly how to compute the update, because then you don't get any of the fun. But you might find the Wikipedia article on Rule 110 interesting.
When you read it, ponder this: This simple rule is "Turing complete" -- which means its capable, perhaps with lots of fussing about, of representing any computation. That simple rule, in itself, is a "computer" in the theoretical sense.
Update
Okay, a little more on the rules. Have a look at the rule table in the Wiki article. What it shows is that you take three cells of your array and determine the next value for the middle one of the three.
So in your rule, you need the array you pass in, t, and an array for the next instant, call it t1.
void rule(int t[]){ // there's the original
int t1[14]; // there's the new array
int ix ; // an index for the loop that's coming up
You want to go through each cell of the array
for(ix=0; ix < 14; ix++){
and check the cell, along with the cells to left and right
if(t[ix-1] == 0 && t[ix] == 0 && t[ix+1] == 0)
t1[ix] = 0;
else if(t[ix-1] == 0 && t[ix] == 0 && t[ix+1] == 1)
t1[ix] = 1;
and so on. You'll need to think about what happens at the edges, ie, when ix == 0
or ix == 13
.
Fincally, you'll need another for
loop to copy t1
back into t
.