So I am attempting an interpretation of Conway's game of life, and apparently I keep attempting to read/write out of bounds, but for the life of me I can't see the problem. Here is where the error occurs:
while(true) {
for (n=1; n<=N; n++) {
for (i=0; i<X; i++)
for (j=0; j<Y; j++) {
for (di=-1; di<=1; di++)
for (dj=-1; dj<=1; dj++) {
//Somewhere in this loop
s=0; if (di ==0 && dj==0) {}
else if (i+di <0 || j+dj <0) {}
else if (i+di>=X || j+dj>=Y) {}
else s=b[i+di][j+dj]==0 ? s:s+1;
}
I wouldn't think it would be necessary, but here is the complete code for anyone interested:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define false 0
#define true 1
int i,j,di,dj;
int X=37,Y=37;
void randb(int **B)/> {
int r;
for (i=0; i<X; i++)
for (j=0; j<Y; j++) {
r = rand_r((unsigned int *)time(NULL)) % 100 + 1;
if (r < 10) b[i][j] = 1;
else b[i][j] = 0;
}
}
/* Check to see if any element of b 1 and b 2 is
* non-equal; if so, return false; else return true. */
int beq(int **b1, int **b2) {
for (i=0; i<X; i++)
for (j=0; j<Y; j++)
if(b1[i][j] != b2[i][j])
return false;
return true;
}
void pboard(int **B)/> {
for (i=0; i<X; i++) {
for (j=0; j<Y; j++)
printf(" %d", b[i][j]);
printf("\n");
}
}
int main() {
int **b = malloc(X*sizeof(int));
for (i=0; i<X; i++)
b[i] = (int *) calloc(Y,sizeof(int));
b[0][25] = b[1][23] = b[1][25] = b[5][23] = b[5][25] = b[8][14] =
b[2][13] = b[2][14] = b[2][21] = b[2][22] = b[2][35] = b[2][36] =
b[3][12] = b[3][16] = b[3][21] = b[3][22] = b[3][35] = b[3][36] =
b[4][1] = b[4][2] = b[4][11] = b[4][17] = b[4][21] = b[4][22] =
b[5][1] = b[5][2] = b[5][11] = b[5][17] = b[5][15] = b[5][18] =
b[6][11] = b[6][17] = b[6][25] = b[7][12] = b[7][16] = b[8][13] = 1;
int n,s;
int N = 10000;
int **nb = malloc(X*sizeof(int));
for (i=0; i<X; i++)
nb[i] = calloc(Y,sizeof(int));
printf("After calloc of new board.\n");
while(true) {
for (n=1; n<=N; n++) {
for (i=0; i<X; i++)
for (j=0; j<Y; j++) {
for (di=-1; di<=1; di++)
for (dj=-1; dj<=1; dj++) {
printf("i: %d; j: %d; di: %d; dj: %d\n", i, j, di, dj);
s=0; if (di ==0 && dj==0) {}
else if (i+di <0 || j+dj <0) {}
else if (i+di>=X || j+dj>=Y) {}
else s=b[i+di][j+dj]==0 ? s:s+1;
}
if (b[i][j] >0 && s>=2) nb[i][j] = 1;
else if (b[i][j]>=0 && s>=3) nb[i][j] = 1;
else nb[i][j] = 0;
} pboard(b=nb);
printf("\nLast step: %d\n", n);
}
}
return 0;
}
So.... any advice or anyone that sees something I don't see would be greatly appreciated. Thanks in advance.

New Topic/Question
Reply




MultiQuote





|