Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rgllm/Elementar-Cellular-Automatons
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: munybt/Elementar-Cellular-Automatons
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Dec 29, 2015

  1. random number

    munybt committed Dec 29, 2015
    Copy the full SHA
    7b8dd69 View commit details
Showing with 94 additions and 61 deletions.
  1. +93 −60 main.c
  2. +1 −1 makefile
153 changes: 93 additions & 60 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,111 +1,144 @@
#include "header.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int validInput (int n, int size){
//return error code: 1-invalid input, 0-valid input
if (n > (pow(2,size)-1)) return 1;
else return 0;
if (n > (pow(2,size)-1)) return 1;
else return 0;
}


void intToBin(unsigned int n, int size, int* binary){
unsigned int mask = 1U << (size-1);
void intToBin(unsigned int n, int* binary){
unsigned int mask = 1U << (8-1);
int i;
for (i = 0; i < size; i++) {
for (i = 0; i < 8; i++) {
binary[i] = (n & mask) ? 1 : 0;
n <<= 1;
}
}


void matrix (int n, int size, int binary[], int start[]){
int GetRand(int min, int max)
{
static int Init = 0;
int rc;

if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min + 1) + min);

return (rc);
}







void iteractions (int n, int size, int binary[], int start[], int niteractions){

int matrix[size][size][size];
int iteraction;
int matrix[2][2][2];
int news[size];
int i;
int i,j;

matrix[0][0][0]=binary[7];
matrix[0][0][1]=binary[6];
matrix[0][1][0]=binary[5];
matrix[1][0][0]=binary[4];
matrix[1][1][0]=binary[3];
matrix[0][1][1]=binary[2];
matrix[1][0][1]=binary[1];
matrix[0][1][1]=binary[4];
matrix[1][0][0]=binary[3];
matrix[1][0][1]=binary[2];
matrix[1][1][0]=binary[1];
matrix[1][1][1]=binary[0];

printf("Number of iteractions: ");
scanf("%d", &iteraction);

//TODO: results not entire correct
for (;iteraction>0;iteraction--){
news[0]=matrix[binary[7]][binary[0]][binary[1]];
news[1]=matrix[binary[0]][binary[1]][binary[2]];
news[2]=matrix[binary[1]][binary[2]][binary[3]];
news[3]=matrix[binary[2]][binary[3]][binary[4]];
news[4]=matrix[binary[3]][binary[4]][binary[5]];
news[5]=matrix[binary[4]][binary[5]][binary[6]];
news[6]=matrix[binary[5]][binary[6]][binary[7]];
news[7]=matrix[binary[6]][binary[7]][binary[0]];
}

//Print the results
for (i=0;i<size-1;i++){
printf("%d - ",news[i]);
}
printf("%d",news[i]);
printf("\n");
for (j=1;j<=niteractions;j++){
news[0]=matrix[start[size-1]][start[0]][start[(1)]];
for(i=1;i<size-1;i++)
news[i]=matrix[start[i-1]][start[i]][start[i+1]];
news[i]=matrix[start[(i-1)]][start[i]][start[0]];

for (i=0;i<size;i++)
start[i]=news[i];

//Print the results
for (i=0;i<size;i++)
if(news[i]==0) printf("□ ");
else printf("■ ");
printf(" t=%d \n",j );

}


}
}


int main (){
int n;
int size;
int i;
int n, size, i, j, niteractions;
char grid;

printf("Hello there! This is a program to calculate Elementar Cellular Automatons.\n");

//Initial number
printf("N (max is 255 for this version): ");
printf(" (max is 255): ");
scanf("%d", &n);

//Size of start. TODO: different sizes
printf("Size (8 for this first version): ");
//Size of start.
printf("Size: ");
scanf("%d", &size);
int binary[size];
int binary[8];
int start[size];

printf("N: %d, Tam: %d\n",n,size);

//validate size
if (validInput(n,size) == 1) {
printf("Increase size.\n"); return 0;
}
else intToBin(n,size,binary);
//Number of iteractions
printf("Number of iteractions: ");
scanf("%d", &niteractions);

//Start array
for (int j=0;j<size;j++){
printf("Grid Position %d : ",(j+1));
printf("Press I to insert the initial grid or R for a random one: ");
fflush(stdin);
scanf(" %c", &grid);
if (grid=='I'){
for (j=0;j<size;j++){
printf("Position %d:",j);
scanf("%d",&start[j]);
}
}
if (grid=='R'){
for (j=0;j<size;j++){
start[j]= GetRand(0, 1);
}
}
//END

//validate size
if (size <= 2) {
printf("Increase size.\n"); return 0;
}
else intToBin(n,binary);
//

//Begin of tests
for (i=0;i<size;i++){
//t=0
for (i=0;i<8;i++){
printf("%d",binary[i]);
}
printf("\n");
for (i=0;i<size-1;i++){
printf("%d - ",start[i]);
}
printf("%d",start[i]);
printf("\n");
//End of Tests

for (i=0;i<size;i++)
if(start[i]==0) printf("□ ");
else printf("■ ");
printf(" t=0 \n \n" );

//t=0


//Iteractions
matrix(n,size,binary,start);
iteractions(n,size,binary,start,niteractions);

return 0;
}
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ CFLAGS=-Wall -O2
OBJS=$(patsubst %.c,%.o,$(wildcard *.c))

main: $(OBJS)
$(CC) $(CFLAGS) -o main $(OBJS)
$(CC) $(CFLAGS) -o main $(OBJS) -lm

clean:
rm bm *.o