-2

我正在尝试编写代码以使用一维数组来显示规则 110,对于 30 个整数的数组,对于 20 行。

#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];
        }


}

它创建一个大小为 30 的数组,其中大部分填充 0 和几个 1,然后在规则函数中创建一个新数组并根据前一个数组包含的内容填充它,然后将其复制到初始数组中,然后将其传递回主要的。但是我的似乎只打印了很多 0。

它应该看起来像:在此处输入图像描述

其中0为空白,1为填充。

4

1 回答 1

1

你的代码中有几个错误,我已经评论过了。我还添加了处理边缘条件的代码。

#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]
    }
}
于 2015-10-10T19:15:32.253 回答