int burn(double prob) { char forest[HEIGHT][WIDTH]; bzero(forest, sizeof(char)*HEIGHT*WIDTH); for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) { float p = 1.0*random()/RAND_MAX; if(p < prob) forest[i][j] = 'T'; } } for (int i = 0; i < HEIGHT; ++i) { if(forest[i][0] == 'T') forest[i][0] = '*'; } int stepCount = 0; while (1) { if(!burnStep(forest)) break; stepCount++; // display(forest); // printf("\n"); } return stepCount+1; }
int main(int argc, char* argv[]) { printf("Trials: %d\n", trials); printf("Width: %d\n", rowsize); printf("Height: %d\n", colsize); clock_t begin, end; double time_spent; begin = clock(); int n; double dp = atof(argv[1]); for(n = 0; n<(int)(1/dp); n++) { srand(time(NULL)); //double p = atof(argv[1]); double p = dp*n; int loopNum = 0; int t; for(t = 0; t<trials; t++) { char forest[rowsize*colsize]; int x; for(x = 0; x<rowsize*colsize; x++) { forest[x] = '-'; if((rand()/(double)RAND_MAX)<p) forest[x] = 'X'; if(x%rowsize==0) forest[x] = '*'; } int done = 0; while(done==0) { int m; done = 1; for(m = 0; m<rowsize*colsize; m++) { if(forest[m] == '*') done = 0; } // sleep(1); // system("clear"); // printGrid(forest); // printf("\n%d\n", loopNum); burnStep(forest); loopNum+=1; } loopNum-=1; } printf(" %f\n", (0.0+loopNum)/(trials*rowsize*colsize)); } end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("Runtime: %f\n", time_spent); printf("\n"); return 0; }