int main(int argc, char **argv){ int col = 0, row = 0; if (argc > 1){ col = strtol(argv[1], NULL, 10); } if (argc > 2){ row = strtol(argv[2], NULL, 10); } if (col <= 0) col = 50; if (row <= 0) row = 50; long long int gen = 0; srand(time(NULL)); unsigned Life[row][col]; for(int c = 0; c < col; c++){ for(int y = 0; y < row; y++){ Life[y][c] = rand() % 2; } } while(1) { show_life(Life, col, row); do_life(Life, col, row); usleep(100000); printf("Generation: [%llu]\n",gen++); } }
/* Function main This function is where the program begins. Initializes the game board and calls do_life to execute the algorithm. Input: int argc - The number of program arguments, including the executable name char **argv - An array of strings containing the program arguments. Output: 0 upon completion of the program */ int main(int argc, char **argv) { /* If argc is at least 2, parse the second element of argv as the number of iterations to run. Otherwise, set the number of iterations to some default value */ int iterations = 10; if(argc>=2) { iterations = (int)argv[1]; } /* Create the game board, a 2D int array. For this lab, you'll want to hard code the array dimensions using preprocessor #define directives as seen in class. Then choose an arbitrary initial configuration for the board (fill each entry with either 0 (a dead cell) or 1 (a live cell)). */ int array[nrows][ncols] = {{1, 1, 0, 1, 0, 1, 0}, {1, 1, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 0, 1, 0}, {1, 1, 1, 1, 0, 1, 0}, {0, 0, 0, 0, 0, 1, 0}, {1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 1, 1}, {1, 0, 1, 0, 0, 1, 1}}; /*int array[nrows][ncols]; int r, c; for(r=0; r<nrows; r++) { for(c=0; c<ncols; c++) { if((r+1)%(c+1)==0) { array[r][c]=1; } else { array[r][c]=0; } } }*/ /* Call the function do_life, passing in the board dimensions, the board itself, and the number of iterations to run the Life algorithm. */ do_life(nrows, ncols, array, iterations); /* Return 0 to show that the program has completed successfully. */ return 0; }