How to resolve the algorithm One-dimensional cellular automata step by step in the C# programming language
How to resolve the algorithm One-dimensional cellular automata step by step in the C# programming language
Table of Contents
Problem Statement
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values. Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation. If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm One-dimensional cellular automata step by step in the C# programming language
This program simulates a one-dimensional cellular automata. Specifically, it simulates a 20-cell automaton with the rule 90 (also known as 170 in Wolfram code, since 90 in base 10 is 170 in base 2). A cellular automaton is a collection of cells that can be in one of a finite number of states, usually 0 or 1. The program starts with a 20-cell automaton with the following initial state:
- - - - - - - - - - - - - - - - -
The program then iterates through 10 generations of the automaton, applying the rule 90 to each cell at each generation. The rule 90 is defined as follows: a cell will be 1 in the next generation if and only if its two neighbors are different. This means that a cell will be 0 in the next generation if and only if its two neighbors are the same.
The program prints the state of the automaton after each generation. The output of the program is as follows:
- - - - - - - - - - - - - - - - -
# - - # - # - - # - - # - - # - -
# # - # - # - # - # - # - # - # -
# - # - # - # - # - # - # - # - #
# # - # - # - # - # - # - # - # -
# - # - # - # - # - # - # - # - #
# # - # - # - # - # - # - # - # -
# - # - # - # - # - # - # - # - #
# # - # - # - # - # - # - # - # -
# - # - # - # - # - # - # - # - #
As you can see, the automaton evolves into a complex and interesting pattern.
Source code in the csharp programming language
using System;
using System.Collections.Generic;
namespace prog
{
class MainClass
{
const int n_iter = 10;
static int[] f = { 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 };
public static void Main (string[] args)
{
for( int i=0; i<f.Length; i++ )
Console.Write( f[i]==0 ? "-" : "#" );
Console.WriteLine("");
int[] g = new int[f.Length];
for( int n=n_iter; n!=0; n-- )
{
for( int i=1; i<f.Length-1; i++ )
{
if ( (f[i-1] ^ f[i+1]) == 1 ) g[i] = f[i];
else if ( f[i] == 0 && (f[i-1] & f[i+1]) == 1 ) g[i] = 1;
else g[i] = 0;
}
g[0] = ( (f[0] & f[1]) == 1 ) ? 1 : 0;
g[g.Length-1] = ( (f[f.Length-1] & f[f.Length-2]) == 1 ) ? 1 : 0;
int[] tmp = f;
f = g;
g = tmp;
for( int i=0; i<f.Length; i++ )
Console.Write( f[i]==0 ? "-" : "#" );
Console.WriteLine("");
}
}
}
}
You may also check:How to resolve the algorithm List comprehensions step by step in the Tcl programming language
You may also check:How to resolve the algorithm Color quantization step by step in the Phix programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Racket programming language
You may also check:How to resolve the algorithm Bell numbers step by step in the EasyLang programming language