Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define L 1000
- #define t_max 100
- int somma(int x,int y); //somma caselle adiacenti
- int c[L][L];
- int c1[L][L];
- int main(){
- int p[t_max];
- int x, y, n, somm, t;
- FILE *fp, *fd, *pop;
- fp=fopen("vita.dat","w");
- fd=fopen("vita.gnu","w");
- pop=fopen("pop.dat","w");
- fprintf(fd, "set size square \n set view map \n plot 'vita.dat' u 1:2");
- fclose(fd);
- for(x=0;x<L+1;x++){ //azzero array
- for(y=0;y<L+1;y++){
- c[x][y]=0;
- c1[x][y]=0;
- }
- }
- for(t=0;t<t_max;t++){
- p[t]=0;
- }
- c[L/2+2][L/2]=1;
- c[L/2+2][L/2+1]=1;
- c[L/2+1][L/2]=1;
- c[L/2+1][L/2-1]=1;
- c[L/2][L/2]=1;
- for(t=0;t<t_max;t++){
- for(x=1;x<L-1;x++){ //stampo
- for(y=1;y<L-1;y++){
- if(c[x][y]==1){fprintf(fp,"%d %d %d %lf\n",x,y,t,p[t]);}
- }
- }
- for(x=1;x<L-1;x++){
- for(y=1;y<L-1;y++){
- somm=somma(x,y); //vedo quanto vale la somma delle celle adiacenti
- if(somm==3 && c[x][y]==0){ //giusta popolazione--->nascita
- c1[x][y]=1;
- }else if(somm!=3 && c[x][y]==0){c1[x][y]=0;}
- if((somm==2||somm==3) && c[x][y]==1){ //sovra o sottopopolazione--->morte
- c1[x][y]=1;
- }else if((somm!=2&&somm!=3) && c[x][y]==1){c1[x][y]=0;}
- }
- }
- for(x=1;x<L-1;x++){ //sostituzione
- for(y=1;y<L-1;y++){
- c[x][y]=c1[x][y];
- if(c1[x][y]==1){p[t]++;}
- }
- }
- fprintf(pop,"%d \n",p[t]);
- }
- system("gnuplot vita.gnu");
- }
- int somma(int x,int y){
- int somma;
- somma=c[x][y+1]+c[x][y-1]+c[x-1][y]+c[x-1][y-1]+c[x-1][y+1]+c[x+1][y]+c[x+1][y-1]+c[x+1][y+1];
- return somma;
- }
Advertisement
Add Comment
Please, Sign In to add comment