As a start:
1. Any 2D array can be represented as a 1d array using a transform using its 2 coordinates.
2. The 8 neighbors plus border element make a 3x3 array, centered on the border element.
int main()
{
int pg[9] = {100,101,102,110,111,112,120,121,122};
typedef int i3[3];
i3* magic = (i3*)(pg);
cout << magic[1][1]; //address pg as if it were 2-d. we mangled it into a 3x3 here
//the reshape tricks only work on solid blocks. dynamic multi-dimensional stuff can't do
//these things.
}
be creative on losing the ifs
1) consider using an enum to name your locations. it will be easier to read crd[top] and then you don't need the comments.
2) you can iterate using these enums. for(i = top, i <= upper_right; i++) if(crd[i] < 0) crd[i]+= gridfields; or something like that. there are other ways too-- you can make what to do for true and false:
int tf[2] = {0,gridfields};
...
crd[4] += tf[crd[4] <0]; //false adds zero, true adds gridfields... no if statement.
not saying any of this is the right way to do it, but you asked. Any or all of these tricks can be useful when fixing pre c98 code, though and getting rid of if statements this way is faster in some code (not all) than using a branch.