-2

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.

It should look like: enter image description here

Where 0 is blank, and 1 is filled.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Finlandia_C
  • 385
  • 6
  • 19

1 Answers1

1

There are several errors in your code which I have commented. I also added code to deal with edge conditions.

#include <stdio.h>

#define WIDTH 30            // don't hard code the dimensions etc.

void rule(int t[]);

int main(void) {
    int count = 0, 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 < WIDTH; i++) {
            printf("%d", t[i]);             // error here was *t
        }
        printf("\n");
        rule(t);                            // moved outside of print loop
    }
    return 0;
}

void rule(int t[]) {
    int t1[WIDTH];
    int ix, lx, rx, i;

    for (ix=0; ix < WIDTH; ix++) {
        lx = (ix + WIDTH - 1) % WIDTH;      // left pixel wraps to other end
        rx = (ix + 1) % WIDTH;              // right pixel wraps to other end

        if ((t[lx] == 0) && (t[ix] == 0) && (t[rx] == 0)) {
            t1[ix] = 0;
        }
        else if ((t[lx] == 0) && (t[ix] == 0) && (t[rx] == 1)) {
            t1[ix] = 1; 
        }
        else if ((t[lx] == 0) && (t[ix] == 1) && (t[rx] == 0)) {
            t1[ix] = 1;
        }
        else if ((t[lx] == 0) && (t[ix] == 1) && (t[rx] == 1)) {
            t1[ix] = 1;
        }
        else if ((t[lx] == 1) && (t[ix] == 0) && (t[rx] == 0)) {
            t1[ix] = 0;
        }
        else if ((t[lx] == 1) && (t[ix] == 0) && (t[rx] == 1)) {
            t1[ix] = 1;
        }
        else if ((t[lx] == 1) && (t[ix] == 1) && (t[rx] == 0)) {
            t1[ix] = 1;
        }
        else 
            t1[ix] = 0;
    }
    for (i = 0; i < WIDTH; i++) {
        t[i] = t1[i];                   // error here was t[ix] = t1[ix]
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • There is a slicker way to calculate the next generation than using all those `if..else`, by building a 3-bit number from `t[lx]`, `t[ix]` and `t[rx]` and then looking up the next value from an 8-element array. – Weather Vane Oct 10 '15 at 19:30