Help me. I am close on, just need help. The ouput for this game of life program is incorrect. I know the problem lies with the "occ" function.
Putting to use what this newbie knows so far, to correct the output I can figure out some of what i need:Code:#include <stdio.h> #define ROW 12 #define COLUMN 17 /*Function prototypes */ int initconfig(int [ROW][COLUMN]); /*initial configuration*/ void printgrid (int[ROW][COLUMN]); /* prints grid */ int occ (int [ROW] [COLUMN], int, int); /*returns # of occupied cells around cells */ void generate (int [ROW] [COLUMN],int [ROW] [COLUMN]); /*creates/prints new generation*/ int main() { int curgen [ROW] [COLUMN]={0}; /* initialize array elements to 0 */ int nextgen [ROW] [COLUMN]={0}; int generation; int i; /* Initialize generation counter */ generation = initconfig (curgen); for (i=1; i<= generation; i++) { printf("Generation #%d \n", i); generate(curgen, nextgen); /* generate next genaration using function while saving current */ } return 0; } int initconfig(int curgen [ROW] [COLUMN]) /* function to generate initial configuration */ { int rowcoord=0; int colcoord=0; int numofgen=0; printf("\n Enter the number of generations: \n"); /* prompt user for number of generations */ scanf("%d", &numofgen); while ((rowcoord != -1)||(colcoord != -1)) /* while loop to prompt user for coordinates until -1 entered */ { printf("Enter coordinates(in row then column format): \n"); printf("Press -1 when done \n"); scanf("%d%d", &rowcoord, &colcoord); if ((colcoord< -1)|| (colcoord==0)||(colcoord>15)) { printf("Invalid row value entered, please reenter.\n"); break; } if ((rowcoord< -1)|| (rowcoord==0)|| (rowcoord >10)) { printf("Invalid column value entered, please reenter both column and row values.\n"); break; } curgen[rowcoord] [colcoord]=1; } printf("\n Initial State \n\n"); printgrid(curgen); return numofgen; } void printgrid(int P[ROW][COLUMN])/* function to print a grid */ { int X; int Y; for (X=1; X<ROW-1; X++) { for (Y=1; Y<COLUMN-1; Y++) if ( P[X][Y]==1) printf("%c",'*'); else printf("%c", '-'); printf("\n"); } } int occ (int curgen[ROW][COLUMN], int R,int C) { int occupied=0; /* initialized occupied counter to 0 */ if ( curgen[R-1][C-1]==1) /* if cell is occupied add one to occupied counter */ ++occupied; if ( curgen[R-1][C]==1) ++occupied; if ( curgen[R][C+1]==1) ++occupied; if ( curgen[R+1][C+1]==1) ++occupied; if ( curgen[R+1][C]==1) ++occupied; if ( curgen[R+1][C-1]==1) ++occupied; if ( curgen[R][C-1]==1) ++occupied; return occupied; } void generate(int curgen[ROW][COLUMN], int nextgen[ROW][COLUMN]) { int r; int c; int occupant; int born=0; int died=0; for (r=1; r <= ROW-2; r++) { for (c=1; c<= COLUMN-2; c++) { occupant= occ(curgen, r, c); if (curgen[r][c]==1 &&(2<=occupant && occupant <=3)) nextgen[r][c]=1; else if (curgen[r][c]==1) { nextgen[r][c]=0; died ++; } else if (curgen[r][c]==0 && occupant==3) { nextgen[r][c]=1; born++; } else nextgen[r][c]=0; } } printf("Number born = %d \t Number died= %d \n \n", born, died); printgrid(nextgen); for (r=1; r<= ROW-2;r++) { for (c=1; c<= COLUMN-2; c++) curgen[r][c] = nextgen[r][c]; } }
Is the above heading in the right direction?? If so, can anyone help me with the coding and conditions (brain drain has me perplexed). Or is there an easier way I am missing or have not learned yet?? Any help would be appreciated. ThanksCode:if (y==0) { if (x==0) (3 conditions/checks ??) if(x==1) ?? (3 conditions/checks??) else (5 conditions/checks??) if(y==14) .... if ( curgen[R-1][C-1]==1) /* if cell is occupied add one to occupied counter */ ++occupied; if ( curgen[R-1][C]==1) ++occupied; if ( curgen[R][C+1]==1) ++occupied; if ( curgen[R+1][C+1]==1) ++occupied; if ( curgen[R+1][C]==1) ++occupied; if ( curgen[R+1][C-1]==1) ++occupied; if ( curgen[R][C-1]==1) ++occupied; return occupied; }



LinkBack URL
About LinkBacks
. I am close on, just need help. The ouput for this game of life program is incorrect. I know the problem lies with the "occ" function. 


