r/cpp_questions • u/xeriax51 • 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
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":
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.
2
u/elperroborrachotoo 21d ago edited 21d ago
Methods:
cell[row-1][col-1]) is valid. Fill the added space with 0.CellAt(int row, int col, int dflt)that returnsdfltifroworcolare 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:Goals:
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/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“).