pietrod21

con conteggio popolazione

Dec 30th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #define L 1000
  3. #define t_max 100
  4.  
  5.  
  6. int somma(int x,int y); //somma caselle adiacenti
  7. int c[L][L];
  8. int c1[L][L];
  9.  
  10.  
  11. int main(){
  12. int p[t_max];
  13. int x, y, n, somm, t;
  14. FILE *fp, *fd, *pop;
  15. fp=fopen("vita.dat","w");
  16. fd=fopen("vita.gnu","w");
  17. pop=fopen("pop.dat","w");
  18. fprintf(fd, "set size square \n set view map \n plot 'vita.dat' u 1:2");
  19. fclose(fd);
  20.  
  21. for(x=0;x<L+1;x++){     //azzero array
  22. for(y=0;y<L+1;y++){
  23.     c[x][y]=0;
  24.     c1[x][y]=0;
  25. }
  26. }
  27. for(t=0;t<t_max;t++){
  28.     p[t]=0;
  29. }
  30. c[L/2+2][L/2]=1;  
  31. c[L/2+2][L/2+1]=1;  
  32. c[L/2+1][L/2]=1;
  33. c[L/2+1][L/2-1]=1;
  34. c[L/2][L/2]=1;
  35.  
  36.  
  37.  
  38.  
  39. for(t=0;t<t_max;t++){
  40.     for(x=1;x<L-1;x++){ //stampo
  41.     for(y=1;y<L-1;y++){
  42.         if(c[x][y]==1){fprintf(fp,"%d %d %d %lf\n",x,y,t,p[t]);}
  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.         if(c1[x][y]==1){p[t]++;}
  60.     }
  61.     }
  62.     fprintf(pop,"%d \n",p[t]);
  63. }
  64. system("gnuplot vita.gnu");
  65. }
  66.  
  67. int somma(int x,int y){
  68.     int somma;
  69.     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];
  70.     return somma;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment