I need help with this hw problem.

Generate a 2-D array of 25 integers and initialize them all to 0. Assign the value 9 to five random locations in the array to represent your mines. Fill the remaining array locations by counting the number of mines in any adjacent squares. Display the filled array.

I got the array filled in with the mines (9's) and the rest with 0's. I'm having trouble filling in the rest of the array with counting the number of mines in any adjacent squares.

I get this
9 0 0 0 0
0 9 0 9 0
0 0 0 0 0
9 0 0 0 0
0 0 9 0 0

When I want this
9 2 2 1 1
2 9 2 9 1
2 2 2 1 1
9 2 1 1 0
1 2 9 1 0

Any help would be appreciated. Thanks

Well, I don't know how exactly get the first array, if you have the code please show me it, but about the second one assuming you got the first one:
This is the code

for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                for (int k = -1; k <= 1; k++) {
                    for (int l = -1; l <= 1; l++) {
                        if (map[i][j] == 9) {
             if ((((i + k) < 5) && ((j + l) < 5))&&(((i + k) >= 0) && ((j + l) >= 0))) {
                                if (map[i + k][j + l] != 9) {
                                
                                    map[i + k][j + l]++;
                        

                                }
                            }
                        }
                    }
                }
            }
        }

i'm thinking something like:
1. scan the whole array for 9's. use two nested loops.
2. if u find a 9, increment all nearby squares by 1 (but do not increment those containing a 9). and be aware that you have to detect if you are currently scanning the TOP-most, BOTTOM-most, LEFT-most or RIGHT-most parts of the array.

suppose, you used m and n as counters and your array was named a:
a[m][n] -> let's say in your scan, you found this square to contain a 9
the nearby squares are:
a[m-1][n-1], a[m-1][n], a[m-1][n+1]
a[m][n-1], -----9-----, a[m][n+1]
a[m+1][n-1], a[m+1][n], a[m+1][n+1]

if those nearby squares are NOT a 9, then increment them by simply:
a[_][_]++
the brackets must be filled with the appropriate indexes which u can find above.

oooh and another complication:
if u found a 9 at the 1st row, last row, leftmost column, or rightmost column
then some parts of those nearby squares i specified above will have to be removed.
example:
scan found a 9 inside a[0][3]:
m==0 means that the scan is currently in the TOP row, and so it doesnt have a nearby square above it. so remove all nearby squares specified above containing "m-1".
this is only for the TOP row condition.

hope this helps :)

Instead of scanning the whole array for 9's I would immediately increment adjacent fields as soon as your random generator chooses a spot.

This will give you the possibility of doing useless increments. (If a mine gets planted on an incremented field) But it saves you the trouble of having to scan the whole array for 9's of which you just had the position.

An alternative would be to temporarily store their positions when they get placed, and use that for easy finding.

And as the user above has stated if you land on an ''edge'' you need to keep out some of the increment instructions.

But I'm sure you can figure out which instructions those are. (eg.: array[0][0] means you shouldn't increment array[x][y-1], array[x-1][y] and array[x-1][y-1])

Instead of scanning the whole array for 9's I would immediately increment adjacent fields as soon as your random generator chooses a spot.

This will give you the possibility of doing useless increments. (If a mine gets planted on an incremented field) But it saves you the trouble of having to scan the whole array for 9's of which you just had the position.

An alternative would be to temporarily store their positions when they get placed, and use that for easy finding.

And as the user above has stated if you land on an ''edge'' you need to keep out some of the increment instructions.

But I'm sure you can figure out which instructions those are. (eg.: array[0][0] means you shouldn't increment array[x][y-1], array[x-1][y] and array[x-1][y-1])

Nice Idea about storing the 9s positions.
but about the 'edge' what if the user can choose the size of the array, it will be difficult figure which position will be the other edge(eg. array[4][4] is an 'edge' here but it is not 'edge' when the array size be 6 by 6 !!)

but you will always know that edge, because the user will input it (or you can use array.length to find it)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.