Okay, so I have tried to write a program that would generate the rule of 110. quoted from wikipedia:

The Rule 110 cellular automaton (often simply Rule 110) is an elementary cellular automaton with the following rule table:
current pattern 111 110 101 100 011 010 001 000
new state for center cell 0 1 1 0 1 1 1 0
In the table above, when the sequence of 1s and 0s corresponding to the new states is regarded as a binary number, the decimal equivalent is 110; hence the name of the rule.

the following is my code:

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int
main()
{
    //set dimensions
    int cols;
    int rows;
    cout<<"how many columns? ";
    cin>>cols;
    cout<<"\nhow many rows? ";
    cin>>rows;
    cout<<"\n";
    int orig[cols];
    int next[cols];
    //set first row with a 1 or 0
    for (int n = 0; n < cols; n++)
    {
        cout<<"Enter the "<<n+1<<"th element(1,0): ";
        int tmp;
        cin>>tmp;
        orig[n] = tmp;
        cout<<"\n";
    }
    for (int m = 0; m < cols; m++) //draw with '*' or ' ' for display's sake
    {
        if (orig[m] == 1)
            cout<<"* ";
        else
            cout<<"  ";
    }
    cout<<"\n";
    int x = 0;
    while(x < rows)//drawing everything else
    {
        int ix;
        for (ix=0; ix < cols; ix++)
        {
            if (ix == 0)//for first element, because there's no element before
            {
                if (orig[ix] == 1 || orig[ix+1] == 1)
                    next[0] = 1;
                else
                    next[0] = 0;
                    orig[ix] = next[ix];
                    if(orig[ix] == 1)
                        cout<<"* ";
                    else
                        cout<<"  ";
                    continue;
            }
            if (ix == cols -1)//for last element, because there's no element afer
            {
                if (orig[ix] == 1)
                    next[cols -1] = 1;
                else
                    next[cols-1] = 0;
                    orig[ix] = next[ix];
                    if(orig[ix] == 1)
                        cout<<"* ";
                    else
                        cout<<"  ";
                    continue;
            }
            //rules
            if (orig[ix-1] == 1 && orig[ix] == 1 && orig[ix+1] == 1)
                next[ix] = 0;
            if (orig[ix-1] == 1 && orig[ix] == 1 && orig[ix+1] == 0)
                next[ix] = 1;
            if (orig[ix-1] == 1 && orig[ix] == 0 && orig[ix+1] == 1)
                next[ix] = 1;
            if (orig[ix-1] == 1 && orig[ix] == 0 && orig[ix+1] == 0)
                next[ix] = 0;
            if (orig[ix-1] == 0 && orig[ix] == 1 && orig[ix+1] == 1)
                next[ix] = 1;
            if (orig[ix-1] == 0 && orig[ix] == 1 && orig[ix+1] == 0)
                next[ix] = 1;
            if (orig[ix-1] == 0 && orig[ix] == 0 && orig[ix+1] == 1)
                next[ix] = 1;
            if (orig[ix-1] == 0 && orig[ix] == 0 && orig[ix+1] == 0)
                next[ix] = 0;
            orig[ix] = next[ix]; //set next as orig
            if(orig[ix] == 1) //draw as '*' or ' '
                cout<<"* ";
            else
                cout<<"  ";
        }
        cout<<"\n";
        x++;
    }
}

when I input cols = 10, rows = 10, and the first line as {0,0,0,0,0,0,0,0,0,1}, I should get a shape that looks like a fractal(http://en.wikipedia.org/wiki/Sierpinski_triangle). instead, some parts of the shape don't generate properly, and I can't tell what's wrong. someone help?

You're replacing your orig[] values as you iterate across your row. You need two loops here, one which computes your next[] values without corrupting your orig[] values, and then another which copies the all of the next[] values back to orig[].

Once you have your code working, you can have fun making it more efficient. How can you avoid doing the copy at all? (Hint: instead of two separately named 1-D arrays, have a 2-D array with 2 rows in it.) Can you think of a way to have only a single 1-D array? (Hint: as you iterate across a row, what's the farthest-left original value you need to maintain?)

Enjoy!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.