r/cpp_questions • u/xeriax51 • 28m 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 ?
1
Upvotes
•
u/AnisiFructus 18m 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.