I’ve been learning C for the last 2 weeks and while I’m still making ‘command line tools’ in Xcode, I figured that’s no reason not to have ’graphics’.
Why not make ASCII versions of some interesting processes. So behold! The elementary cellular automata as described on the Wolfram Mathworld site.

The code:
#include <stdio.h>
#include <unistd.h> //for the sleep function
#include "dec2bin.h" //function to get the binary equivalent of the rule
#include <string.h> //for the string comparison
int decimal;
char binary[9];
char neighbors[4];
char bin1[4] = "111";
char bin2[4] = "110";
char bin3[4] = "101";
char bin4[4] = "100";
char bin5[4] = "011";
char bin6[4] = "010";
char bin7[4] = "001";
char bin8[4] = "000";
char singleLine[31], lineTemp[31];
int i, j, iMin, iPlus, stringTest;
void applyRules();
/****************************************************************/
//Function to initiate the line to all 0, except the center one
/****************************************************************/
void initLine(){
for (i = 0; i < 31; i++) {
singleLine[i] = ’0′;
lineTemp[i] = ’0′;
}
singleLine[15] = ’1′;
lineTemp[15] = ’1′;
}
/****************************************************************/
//Function to print the line
/****************************************************************/
void printLine(){
for (i = 0; i < 31; i++) {
if (singleLine[i] == ’0′) {
printf("__");
}
else if (singleLine[i] == ’1′){
printf("[]");
}
}
printf("\n");
applyRules();
}
/****************************************************************/
//Function to apply the rules
/****************************************************************/
void applyRules(){
for (i = 0; i < 31; i++) {
if (i == 0){
iMin = 30;
iPlus = i+1;
}
if (i == 30){
iPlus = 0;
iMin = i-1;
}
else if (i != 0 && i != 30) {
iMin = i-1;
iPlus = i+1;
}
neighbors[0] = singleLine[iMin];
neighbors[1] = singleLine[i];
neighbors[2] = singleLine[iPlus];
if (strcmp(neighbors, bin1) == 0){
lineTemp[i] = binary[0];
}
else if (strcmp(neighbors, bin2) == 0){
lineTemp[i] = binary[1];
}
else if (strcmp(neighbors, bin3) == 0){
lineTemp[i] = binary[2];
}
else if (strcmp(neighbors, bin4) == 0){
lineTemp[i] = binary[3];
}
else if (strcmp(neighbors, bin5) == 0){
lineTemp[i] = binary[4];
}
else if (strcmp(neighbors, bin6) == 0){
lineTemp[i] = binary[5];
}
else if (strcmp(neighbors, bin7) == 0){
lineTemp[i] = binary[6];
}
else if (strcmp(neighbors, bin8) == 0){
lineTemp[i] = binary[7];
}
}
for (j = 0; j < 31; j++) {
singleLine[j] = lineTemp[j];
}
usleep(50000);
printLine();
}
/****************************************************************/
/****************************************************************/
int main (int argc, const char * argv[]) {
initLine();
printf("Which rule to use (0 – 255)?\n");
scanf("%i", &decimal);
dec2bin(decimal, binary);
printf("\n The binary value of %i is %s \n",decimal,binary);
printLine();
return 0;
}
and the required library :
void dec2bin
(int decimal
, char *binary
)
{
int k
= 0, n
= 0;
int remain
;
char temp
[9];
// if the input is wrong, abort and give error message
if (decimal
< 0 || decimal
> 256)
{
printf("error: rule # must be between 0 and 255)\n");
return;
}
do
{
remain
= decimal
% 2;
decimal
= decimal
/ 2;
temp
[k
++] = remain
+ ’0′;
} while (k
< 8);
// reverse the spelling
while (k
>= 0)
binary
[n
++] = temp
[–k
];
binary
[n
-1] = 0;
}