1 Replies - 504 Views - Last Post: 08 October 2012 - 10:45 PM Rate Topic: -----

#1 Aerion4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-November 11

cellular automata rule 30

Posted 08 October 2012 - 07:49 PM

I have been given a skeleton code, and it has been verified as accurate, i just need help with the finer details. I cant figure out how to write the update_row function into another array, and they save that array into the global array, and start the process over again. Any insight would be appreciated.


#include <iostream>
#include <cstdlib>
using namespace std;

void setup_row();
void print_row();
void update_row();
int find_next_cell_state(int left_neighbor, int cell, int right_neighbor);


const int WIDTH=80;
int row[WIDTH];
int array[WIDTH];

int main()
{
setup_row();
print_row();


for (int i = 0; 42 > i ; i++)
{
update_row();
print_row();
}

}

// The setup_row function creates an initial state for the cellular autonoma
void setup_row()
{
for(int i=0; 38 > i; i++)
{
row[i] = 0;
}

row[39] = 1;

for(int i = 40; 79 > i; i++)
{
row[i] = 0;
}
}

// The print_row function outputs the state of the row to the user. For all
// cells that are 0, it outputs a space (" "), for all cells that are 1, it
// outputs a star ("*")
void print_row()
{
for(int i =0; i < 80; i++)

{
if( row[i] == 0)
cout<<" ";
if (row[i] == 1)
cout<<"*";
}



}

// The update_row function updates the contents of the row after calculating
// the next states using the find_next_cell_state function
void update_row()
{
for (int i =80; i < 80; i++)
{
int left_neighbor, cell, right_neighbor, a;
left_neighbor = row[0] +=3;
cell = row[1] +=3;
right_neighbor = row[2] +=3;


int array[a] = find_next_cell_state( left_neighbor, cell, right_neighbor);

}
}





// The find_next_cell_state function implements the Rule 30 celular autonoma rules.
// The states for this are:
//
// 111 110 101 100 011 010 001 000
// 0 0 0 1 1 1 1 0

int find_next_cell_state(int left_neighbor, int cell, int right_neighbor)
{
if (0 == cell)
{
if (1 == (left_neighbor + right_neighbor))
{
return 1;
}
else
{
return 0;
}
}
else
{
if (1 == left_neighbor)
{
return 0;
}
else
{
return 1;
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: cellular automata rule 30

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1764
  • View blog
  • Posts: 3,419
  • Joined: 30-May 10

Re: cellular automata rule 30

Posted 08 October 2012 - 10:45 PM

Was this code indented properly when it was "given" to you?
http://sourceforge.n...tle=Indentation

> 021 for (int i = 0; 42 > i ; i++)
> 032 for(int i=0; 38 > i; i++)
> 039 for(int i = 40; 79 > i; i++)
> 067 for (int i =80; i < 80; i++)
You might wonder how many times the last one is going to run.


I see that the original author subscribes to this particular piece of brain-rot
http://c-faq.com/style/revtest.html

The swapping rule was only ever designed to trap var = 1 when you really meant var == 1. The ancient trick was to write 1 == var, in the hope that if 1 = var was written by mistake, it would get an error message.

Which is all well and good, except that.
1. Most modern compilers will generate warnings anyway, without destroying the code.
$ cat foo.c
#include <stdio.h>
int main ( int argc, char *argv[] ) {
    if ( argc = 1 ) {
    }
    return 0;
}

$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:3:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]



2. It only works for = vs. ==. So changing i < 80 into 80 > i only serves to destroy readability, without adding any diagnostic benefit. You'll spend more time thinking about whether these inverted tests are correct to even bother about fixing the wider issues. See your line 67 for an example in practice.

3. Swapping fails at the most critical moment. If you have var1 = var2, when you meant var1 == var2, then swapping things round to var2 = var1 still leaves you in the same sinking boat as before. Great, you remembered the rule, but you forgot the point of the exercise (and you don't get an error message).
Fortunately, modern compilers also spot this as well.
$ cat foo.c
#include <stdio.h>
int main ( int argc, char *argv[] ) {
    if ( argc = argv[0][0] ) {
    }
    return 0;
}

$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:3:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]



4. If the natural left component was not an l-value to begin with, then swapping it over would achieve nothing. Eg if ( strlen(s) = var ) would get an error message, but if ( var = strlen(s) ) falls into the tar pit.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1