Your problem could be with
int x,y;
for(x=1;x<=MAX;x++)
for(y=1;y<=MAX;y++)
gptr[x][y] = 0;
Your accessing element outside your array bounds. Probably should be:
int x,y;
for(x=0;x<MAX;x++)
for(y=0;y<MAX;y++)
gptr[x][y] = 0;
The same goes in your other arrays/loops. C/C++ start counting a 0, unlike FORTRAN which starts counting a 1 (why, just why...)