I'm trying to write code to use 1D arrays to show rule 110, for an array of 30 integers, for 20 lines.
#include <stdio.h>
void rule(int t[]);
int main(void)
{
int count = 0;
int i;
int t[] = {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0};
while (count++ < 20)
{
for (i = 0; i < 30; i++)
{
rule(t);
printf("%d", *t);
}
printf("\n");
}
return 0;
}
void rule(int t[])
{
int t1[30];
int ix;
int i;
for (ix=0; ix < 30; ix++)
{
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;
}
else if ((t[ix-1] == 0) && (t[ix] == 1) && (t[ix+1] == 0))
{
t1[ix] = 1;
}
else if ((t[ix-1] == 0) && (t[ix] == 1) && (t[ix+1] == 1))
{
t1[ix] = 1;
}
else if ((t[ix-1] == 1) && (t[ix] == 0) && (t[ix+1] == 0))
{
t1[ix] = 0;
}
else if ((t[ix-1] == 1) && (t[ix] == 0) && (t[ix+1] == 1))
{
t1[ix] = 1;
}
else if ((t[ix-1] == 1) && (t[ix] == 1) && (t[ix+1] == 0))
{
t1[ix] = 1;
}
else if ((t[ix-1] == 1) && (t[ix] == 1) && (t[ix+1] == 1))
{
t1[ix] = 0;
}
}
for (i = 0; i < 30; i++)
{
t[ix] = t1[ix];
}
}
It creates an array size 30 filled with mostly 0's and a couple of 1's, then in the rule function it creates a new one and fills it based on what the previous array contains, then copies this into the initial array and this is passed back to main. However mine only seems to print lots of 0's.
Where 0 is blank, and 1 is filled.