Hi, Im quite new to programming. I have a change that Im trying to implement in a program that I inherited. Its frustrating because I imagine for an experienced programmer its pretty straightforward. Can anyone help me?

I have an array of squares. Each square has a value, either s(i,j)=1 or s(i,j)=-1. I want to randomly select a square and a random neighbor to the top, bottom, left, or right such that the neighbor has an opposite value to the original square, else move on to the next random square and look for a random neighbor that has the opposite value, etc. Im not sure how to do this in an efficient way, though the program already has a random integer generator, App.RandInt

Some potential problems:

-Im not sure if I would need to take into account boundary conditions. While for periodic boundaries, this probably wouldnt be a problem, for isolated boundaries it might be, where the edge squares have some neighbors that are off the array completely, i.e. whose value s(i,j) = 0.

-Finally, I will use this to then to call another subroutine, so it seems impractical to call it within each if statement - is there another way?

I started with the following, but I know its wrong and it seems very inefficient:

```
Dim i,j as Integer
i = RandInt(Size) // this calls the preexisting App.RandInt to generate a random integer between zero and Size-1 inclusive
j = RandInt(Size)
// The following looks at all the neighbors. I want it to look at a random one, i.e. not necessarily the one to the top (for example) else move on. Am not sure how to do that, though
if s(i,j) <> s(i+1,j) then
if s(i,j) <> 0 and s(i+1,j) <> 0 then
//call subroutine
end if
end if
if s(i,j) <> s(i-1,j) then
if s(i,j) <> 0 and s(i-1,j) <> 0 then
//call subroutine
end if
end if
if s(i,j) <> s(i,j+1) then
if s(i,j) <> 0 and s(i,j+1) <> 0 then
//call subroutine
end if
end if
if s(i,j) <> s(i,j-1) then
if s(i,j) <> 0 and s(i,j-1) <> 0 then
//call subroutine
end if
end if
```