r/cpp_questions • • 22d ago

OPEN How could i do operations on multidimensional Arrays where the Cell might not be there ?

One of the assignements for home our professor gave us was :

-a 10x10 Matrix using multidimensional arrays

-the matrix contains all 1s and 0s

-count how many "Islands" there are (a 1 that's Has only 0s next to It in every direction)

My problem Is, i could do a pretty long line for where It checks every generic Cell [ ] [ ] next to One given, but what if that Cell Is in a position where it doesnt have cells next to It In some directions, for example, if the 1 was in a corner ?

3 Upvotes

10 comments sorted by

7

u/thedaian 22d ago

You know the dimensions of the arrays. You know which cell you're currently at, and which cell you're checking next. So, if the next cell would be outside the array, just skip it.

The best way to do all of this depends on the requirements of the assignment, and how far along you've gotten with C++, but an easy solution is to just have a function that checks the next cell and returns a 1 or 0, then you can quickly return from it if the next cell would be outside the matrix.

3

u/alfps 22d ago

You need a strict definition of "island":

  • a cell that is 1 with no adjacent 1, including diagonally, or
  • a call that is 1 with 0 on all sides including diagonally.

For the first definition you can pretend that there's a border of cells around the matrix where every one is 0. For the second definition you can pretend that every cell in the border is 1. Now just define an access function that produces these results for coordinates just outside the matrix and otherwise the value of the specified cell in the matrix, and use that access function for checking the neighbours of any given cell.

Or, instead of the access function copy the 10×10 matrix to the middle of a 12×12 matrix and check that.

1

u/thingerish 22d ago

This, but I would make a properly initialized 12x12 and write the data into [1.11] x [1.11], then evaluate.

1

u/Independent_Art_6676 22d ago

and what about 1s on the edges, where there isn't a place for the zero? Island or not?

anyway, one way as stated is to make the matrix larger than needed so the 'edges' are safe to look at. But that might give a false island, if you set them to zeros, depending on the above question. Setting to 1 might be OK, again, depending on the above? This is probably the easiest and most efficient way, but if you are hard locked to a 10x10 (getting it passed into your program from elsewhere, for example) you would need to do something else or pay an expensive copy to upsize it. Something else means either checking bounds each time (yuck) or doing the edges separately with a specialty routine that checks bounds and doing the inner cells with the no-check version, or similar ideas.

you might find the logic used in minesweeper game enlightening.

2

u/alfps 22d ago

❞ and what about 1s on the edges, where there isn't a place for the zero? Island or not?

Depends on the definition. With the first definition it can be an island. With the second definition it can't.

That's why a strict definition is needed.

2

u/elperroborrachotoo 21d ago edited 21d ago

Methods:

  • Add a "curb" around the matrix. Instead of the range (0...N), allow the range (-1 … N+1). Then every off-by-one access (e.g., cell[row-1][col-1]) is valid. Fill the added space with 0.
  • a picker method, int CellAt(int row, int col, int dflt) that returns dflt if row or col are out of range.

(the first is a bit more efficient, relevant if you do that query thousands or millions of times. The second one is easier to adapt to existing data structures, and a bit more flexible for other queries.)

Combine one of that with a list of relative neighbor addresses:

std::array<std::pair<int,int>> Neighbors = { {-1,0, }, { 0, -1}, … };

Adjust, depending on whether diagonals count or not.

Then accessing all neighbors of {row, col} will be rather trivial:

for(auto & n : Neighbors)
{
   auto neighbor = CellAt(row + n.first, col + n.second, <default if out of range>);
   …
}

Goals:

  • separate the definition of "what's a neighbor" from "what do I want to do with them", and avoid long statements everytime you have to go through all of them
  • solve the out-of-bounds problem, so that we can treat every cell the same, whether it's center, rim or corner.

1

u/AnisiFructus 22d ago

You could do 2 nested for loops for -1,0,1 (which areadded to the actual position coordinatewise) and continue for 0,0 and for the case when you would step out of the matrix. I think it's also easier to read then if it were in one line. If you insist for the one line solution, you could do it with the ternary operator.

Note that it's not clear that the 1s at the edges could count as islands, if not then it's even simpler, just don't go to the edges.

1

u/_abscessedwound 22d ago

This is just a disguised BFS question, no? Order the elements via BFS, then prune any nodes of weight 0. The islands will just “fall out” afterwards.

The array gives us the relationships that define the vertices.

1

u/Discordbothost 21d ago

for OP's definition, two adjacent 1s surrounded by zeros should give zero islands. connected-component BFS would count one. i'd check that example against the assignment wording first; counting isolated cells and counting connected groups are different tasks.

1

u/MatthiasWM 22d ago

The professor wants you to learn two things: loop over a multi dimensional array the test each cell, and find border conditions: for example, the leftmost cell has no left neighbor, so the island test code must check for border conditions and handle them correctly before accessing an invalid address.

Probably not part of the assignment, but if you modify islands based on what you found, make sure that you don’t write the same array that you read or you will influence the results (see “Game of Life“).