pietrod21

Untitled

Dec 29th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #define L 2000
  3. #define t_max 5200
  4.  
  5.  
  6. int somma(int x,int y); //somma caselle adiacenti
  7. int c[L][L];
  8. int c1[L][L];
  9.  
  10. int main(){
  11. int x, y, n, somm, t;
  12. FILE *fp, *fd;
  13. fp=fopen("vita.dat","w");
  14. fd=fopen("vita.gnu","w");
  15. fprintf(fd, "set size square \n set view map \n plot 'vita.dat' u 1:2");
  16. fclose(fd);
  17.  
  18. for(x=0;x<L+1;x++){     //azzero array
  19. for(y=0;y<L+1;y++){
  20.     if(x%%)              
  21.     c[x][y]=0;
  22.     c1[x][y]=0;
  23. }
  24. }
  25. c[L/2+2][L/2+1]=1;  
  26. c[L/2+1][L/2+3]=1;
  27. c[L/2][L/2]=1;
  28. c[L/2][L/2+1]=1;
  29. c[L/2][L/2+4]=1;
  30. c[L/2][L/2+5]=1;
  31. c[L/2][L/2+6]=1;
  32.  
  33.  
  34.  
  35.  
  36.  
  37. for(t=0;t<t_max;t++){
  38.     for(x=1;x<L-1;x++){ //stampo
  39.     for(y=1;y<L-1;y++){
  40.         if(c[x][y]==1){fprintf(fp,"%d %d %d\n",x,y,t);}
  41.  
  42.     }
  43.     }
  44.  
  45.     for(x=1;x<L-1;x++){
  46.     for(y=1;y<L-1;y++){
  47.         somm=somma(x,y);    //vedo quanto vale la somma delle celle adiacenti
  48.         if(somm==3 && c[x][y]==0){ //giusta popolazione--->nascita
  49.             c1[x][y]=1;
  50.         }else if(somm!=3 && c[x][y]==0){c1[x][y]=0;}
  51.         if((somm==2||somm==3) && c[x][y]==1){ //sovra o sottopopolazione--->morte
  52.             c1[x][y]=1;
  53.         }else if((somm!=2&&somm!=3) && c[x][y]==1){c1[x][y]=0;}
  54.     }
  55.     }
  56.     for(x=1;x<L-1;x++){ //sostituzione
  57.     for(y=1;y<L-1;y++){
  58.         c[x][y]=c1[x][y];
  59.     }
  60.     }
  61. }
  62. system("gnuplot vita.gnu");
  63. }
  64.  
  65. int somma(int x,int y){
  66.     int somma;
  67.     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];
  68.     return somma;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment