/* * Created by Dr. Xin Yuan for the COP5570 class * * To compile to test performance: * gcc -O3 -DNOOUTPUTFILE proj4_seq.c * * To compile to test correctness (the program will output * file final_world000.txt for comparison: * gcc proj4_seq.c * */ #include #include #ifdef NOOUTPUTFILE #define NOOUTPUTFILE 1 #else #define NOOUTPUTFILE 0 #endif #define MAX_N 8192 #ifndef DEBUG_LEVEL #define DEBUG_LEVEL 0 #endif char w[MAX_N][MAX_N]; char neww[MAX_N][MAX_N]; int w_X, w_Y; void init(int X, int Y) { int i, j; w_X = X, w_Y = Y; for (i=0; i=w_X)) { printf("neighborcount: (%d %d) out of bound (0..%d, 0..%d).\n", x,y, w_X, w_Y); exit(0); } if ((y<0) || (y >=w_Y)) { printf("neighborcount: (%d %d) out of bound (0..%d, 0..%d).\n", x,y, w_X, w_Y); exit(0); } if (x==0) { if (y == 0) { count = w[y][x+1] + w[y+1][x] + w[y+1][x+1]; } else if (y == w_Y-1) { count = w[y][x+1] + w[y-1][x] + w[y-1][x+1]; } else { count = w[y-1][x] + w[y+1][x] + w[y-1][x+1] + w[y][x+1] + w[y+1][x+1]; } } else if (x == w_X -1) { if (y == 0) { count = w[y][x-1] + w[y+1][x-1] + w[y+1][x]; } else if (y == w_Y-1) { count = w[y][x-1] + w[y-1][x] + w[y-1][x-1]; } else { count = w[y-1][x] + w[y+1][x] + w[y-1][x-1] + w[y][x-1] + w[y+1][x-1]; } } else { /* x is in the middle */ if (y == 0) { count = w[y][x-1] + w[y][x+1] + w[y+1][x-1] + w[y+1][x] + w[y+1][x+1]; } else if (y == w_Y-1) { count = w[y][x-1] + w[y][x+1] + w[y-1][x-1] + w[y-1][x] + w[y-1][x+1]; } else { count = w[y-1][x-1] + w[y][x-1] + w[y+1][x-1] + w[y-1][x] + w[y+1][x] + w[y-1][x+1] + w[y][x+1] + w[y+1][x+1]; } } return count; } int main(int argc, char *argv[]) { int x, y; int iter = 0; int c; int init_count; int count; if (argc == 1) { printf("Usage: ./a.out w_X w_Y\n"); exit(0); } else if (argc == 2) test_init(); else /* more than three parameters */ init(atoi(argv[1]), atoi(argv[2])); c = 0; for (x=0; x 10) print_world(); for (iter = 0; (iter < 200) && (count <50*init_count) && (count > init_count / 50); iter ++) { for (x=0; x < w_X; x++) { for (y=0; y=4) neww[y][x] = 0; /* die of overpopulation */ else if (c == 3) neww[y][x] = 1; /* becomes alive */ else neww[y][x] = w[y][x]; /* c == 2, no change */ } } /* copy the world, and count the current lives */ count = 0; for (x=0; x 10) print_world(); } if (NOOUTPUTFILE != 1) { FILE *fd; if ((fd = fopen("final_world000.txt", "w")) != NULL) { for (x=0; x