What is the problem with my code? Please help...

RealBiggie

Recruit
[font=helvetica, arial, verdana,]Hi![/font]

[font=helvetica, arial, verdana,]My homework is to make a c program for game of life. I have made my code already, the problem is when i'm running the code the compiler doesn't display any error, but my code still doesn't work well. I have to print in the screen an initial generation coming from a textfile, from that initial generation, I have to calculate the next generation using the rules of the game. After calculating the next generation I should save it from another textfile.[/font]

[font=helvetica, arial, verdana,]Rules of the game: [/font]

[font=helvetica, arial, verdana,]1. An organism will survive if it has exactly 2 or 3 neighbors.[/font]

[font=helvetica, arial, verdana,]2. An organism will die if it has less than 2 or more than 3 neighbors.[/font]

[font=helvetica, arial, verdana,]3. An organism will be born if it has exactly 3 neighbors.[/font]

[font=helvetica, arial, verdana,]I think I applied the rules of the game well, however I have no idea why my code still can not calculate the next generation.[/font]

[font=helvetica, arial, verdana,]I hope you guys could help me.. Thanks a lot!! by the way my board is a 25x25 matrix of the character 'X' and 'blank space'. 'X' corresponds to a living organism.[/font]

here is my code:

#include <stdio.h>

char initial [50][50];

char generation1 [50][50];

int counting_neighbors (int i, int j) {

int neighborscount;

neighborscount=0;

if (initial [j+1]=='X')

neighborscount ++;

if (initial [j-1]=='X')

neighborscount ++;

if (initial [i-1][j]=='X')

neighborscount ++;

if (initial [i-1][j+1]=='X')

neighborscount ++;

if (initial [i-1][j-1]=='X')

neighborscount ++;

if (initial [i+1][j]=='X')

neighborscount ++;

if (initial [i+1][j+1]=='X')

neighborscount ++;

if (initial [i+1][j-1]=='X')

neighborscount ++;

return neighborscount;

}

int main() {

int i, j;

int neighbors;

char filename[10], initialgen[26];

char *g;

FILE *fp1;

printf ("Enter filename of initial generation: ");

scanf ("%s", filename);

fp1=fopen(filename, "r");

i=0;

j=0;

do {

g = fgets (initialgen, 26, fp1);

for(j=0;j<26;j++){

initial [j]=g[j];

}

i++;

if (g!=NULL)

printf ("%s", initialgen);

}

while (g != NULL);

fclose (fp1);

printf ("\n");

neighbors=0;

for(i=0;i<25;i++) {

for(j=0;j<26;j++){

neighbors=counting_neighbors(i,j);

if (initial [j]=='X') {

if (neighbors==3)

generation1 [j]='X';

else

generation1 [j]=' ';

if (neighbors==2)

generation1 [j]='X';

else

generation1 [j]=' ';

}

if(initial [j]==' ') {

if (neighbors==3)

generation1 [j]='X';

else

generation1 [j]=' ';

}

}

}

fp1=fopen ("gen1.txt", "w");

if (fp1!=NULL) {

for(i=0;i<25;i++) {

for(j=0;j<26;j++){

fprintf (fp1, "%c", generation1 [j]);

}

fprintf (fp1, "\n");

}

}

fclose(fp1);

printf ("Generation1:");

for(i=0;i<25;i++) {

for(j=0;j<26;j++) {

printf ("%c", generation1 [j]);

} printf ("\n");

}

return 0;

}[font=helvetica, arial, verdana,] [/font]

[font=helvetica, arial, verdana,] [/font]
 
neighborscount=0;

if (initial [j+1]=='X')

neighborscount ++;

if (initial [j-1]=='X')

neighborscount ++;

if (initial [i-1][j]=='X')

neighborscount ++;

if (initial [i-1][j+1]=='X')

neighborscount ++;

if (initial [i-1][j-1]=='X')

neighborscount ++;

if (initial [i+1][j]=='X')

neighborscount ++;

if (initial [i+1][j+1]=='X')

neighborscount ++;

if (initial [i+1][j-1]=='X')


every time function called you are initiating neighbors count to 0 ,and irrespective of X incrementing it
 
int counting_neighbors (int i, int j) {

int neighborscount;

neighborscount=0;

if (initial [j+1]=='X')

neighborscount ++;

if (initial [j-1]=='X')

neighborscount ++;

if (initial [i-1][j]=='X')

neighborscount ++;

if (initial [i-1][j+1]=='X')

neighborscount ++;

if (initial [i-1][j-1]=='X')

neighborscount ++;

if (initial [i+1][j]=='X')

neighborscount ++;

if (initial [i+1][j+1]=='X')

neighborscount ++;

if (initial [i+1][j-1]=='X')

neighborscount ++;

return neighborscount;

}


You are not checking for array bounds. that will crash the program. e.g., i=0 j=0; initial [j-1] will be initial [0][-1] which will result in access voilation.
 
Back
Top