Created
August 25, 2014 20:03
-
-
Save calebrob6/f82c5ab0204eb9a316c9 to your computer and use it in GitHub Desktop.
Automata in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <malloc.h> | |
int main(int argc, char* argv[]) | |
{ | |
/* | |
char *name = "test.txt"; | |
FILE *file; | |
char *buffer; | |
unsigned long fileLen; | |
file=fopen(name,"rb"); | |
if(!file){ | |
fprintf(stderr,"Unable to open file %s",name); | |
return; | |
} | |
fseek(file,0,SEEK_END); | |
fileLen=ftell(file); | |
fseek(file,0,SEEK_SET); | |
buffer=(char *)malloc(fileLen+1); | |
if(!buffer){ | |
fprintf(stderr, "Memory error!"); | |
fclose(file); | |
return; | |
} | |
fread(buffer,fileLen,1,file); | |
fclose(file); | |
free(buffer); | |
printf("Length of %s is %d\n",name,fileLen); | |
printf("sizeof(char) is %d\n",sizeof(char)); | |
*/ | |
srand(time(NULL)); | |
int r = rand(); | |
long n = 800000000; | |
long randomFill = n; | |
int numIterations = 20; | |
unsigned char* board = (unsigned char*) malloc(n); | |
if(!board) printf("Something happened...\n"); | |
long i,j; | |
//clear board | |
printf("Clearing the board...\n"); | |
for(i=0;i<n;i++){ | |
board[i]=0; | |
} | |
printf("Random fill...\n"); | |
for(i=0;i<randomFill;i++){ | |
board[i] = rand()%2; | |
} | |
unsigned char firstVal = 0; | |
unsigned char endVal = 0; | |
unsigned char prevVal = 0; | |
unsigned char tempVal = 0; | |
long counter = 0; | |
for(j=0;j<n;j++){ | |
if(board[j]==1) counter++; | |
} | |
printf("Initial number of 1's: %d\n",counter); | |
for(i=0;i<numIterations;i++){ | |
printf("Iteration: %d/%d\n",i,numIterations); | |
//board initial state | |
firstVal = board[0]; | |
endVal = board[n-1]; | |
//do first iteration | |
prevVal = board[0]; | |
board[0] = endVal^(board[0]|(~board[1])); | |
for(j=1;j<n-1;j++){ //do all 1...n-1 iterations | |
tempVal = board[j]; | |
board[j] = prevVal^(board[j]|(~board[j+1])); | |
prevVal = tempVal; | |
} | |
//do last iteration | |
board[n-1] = prevVal^(board[n-1]|(~firstVal)); | |
//for(j=0;j<n;j++){ | |
// printf("%3d ",board[j]); | |
//} | |
//printf("\n"); | |
} | |
counter = 0; | |
for(j=0;j<n;j++){ | |
//printf("%d ",board[j]); | |
if(board[j]==1) counter++; | |
} | |
printf("Final number of 1's: %d\n",counter); | |
system ("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment