i think i did the part "countLiveCells " at bottom last is wrong,would u give give me any idea ?thanks.Code:#include #include #define MAX_SIZE 10 void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size); void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size); void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size); int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size); void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size); int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size); void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size);intmain() {intlife_grid[MAX_SIZE][MAX_SIZE];intsize = 5; zapGrid(life_grid,size); enterGrid(life_grid,size); printGrid(life_grid, size);do{ printf("\n"); calcNextGen( life_grid, size); printGrid( life_grid, size); printf("Number of live cells = %d\n", countLiveCells (life_grid, size)); system("pause"); }[d]while[/b](2 > 1); return 0; }voidprintNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size) {inti,j;/*print it out*/for(i = 0; i < size; ++i) {for(j = 0; j < size; ++j) { printf("%2d", countNeighbors(i,j,grid, size)); } printf("\n"); } }voidprintGrid(int grid[MAX_SIZE][MAX_SIZE],int size) {inti,j;/*print it out*/for(i = 0; i < size; ++i) {for(j = 0; j < size; ++j) { printf("%2d", grid[i][j]); } printf("\n"); } }voidzapGrid(int grid[MAX_SIZE][MAX_SIZE],int size) {inti, j;/*fill it with all zeros */for(i = 0; i < size; ++i) {for(j = 0; j < size; ++j) { grid[i][j] = 0; } } }voidenterGrid(int grid[MAX_SIZE][MAX_SIZE],int size) {introw, col; printf("Please enter row and col for life or -1 -1 to stop\n");do{ scanf("%d%d", &row, &col); if (row > -1 && col > -1) grid[row][col] = 1; }while(row > -1 && col > -1); }intcountNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size) {intcount = 0;/* top row */if (grid[row -1 ][col-1] == 1) count += 1; if (grid[row -1 ][col] == 1) count += 1; if (grid[row -1 ][col+1] == 1) count += 1;/*middle row */if (grid[row ][col-1] == 1) count += 1; if (grid[row ][col+1] == 1) count += 1;/* bottom row */if (grid[row +1 ][col-1] == 1) count += 1; if (grid[row +1 ][col] == 1) count += 1; if (grid[row +1 ][col+1] == 1) count += 1; return count; }voidcalcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size) {intnew_grid[MAX_SIZE][MAX_SIZE];intcount,i, j; zapGrid(new_grid, size);for(i = 0; i < size; ++i) {for(j = 0; j < size; ++j) { count = countNeighbors(i, j, grid, size);if(grid[i][j] == 1) {/*live cell, does is keep living? */if(count == 2 || count == 3) new_grid[i][j] = 1;/*congratulations on making it to the next generation */}else{/*dead cell, is there birth?*/if(count == 3) new_grid[i][j] = 1; /*birth!!!! try to hold back the tears */ } }/*for j*/ }/*for i*/ /*copy the new back over the old */for(i = 0; i < size; ++i) {for(j = 0; j < size; ++j) { grid[i][j] = new_grid[i][j]; } } }intcountLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size) {intcount=0 ;inti;for(i=0 ; i< size; ++i) count = count + size;returncount; }

p/s : and how do i make the code to ask the user if they want to see the next generation (so they can exit ).thanks alot

Tweet This+ 1 thisPost To Linkedin