How to go around the perimeter of a rectangular matrix in one cycle?

  • Use the extreme cell index as an option. Add your work (your code), otherwise the question will rather be closed as a training - LFC
  • Study assignments are allowed as questions only under the condition that you tried to solve them yourself before asking a question. Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote trying to solve the problem - LFC
  • Here is ru.stackoverflow.com/a/761797/182825 I gave the implementation of the "ring iterator" for the matrix. Such an iterator allows you to go around the ring in the matrix (including along the perimeter) “in one cycle”. - AnT

1 answer 1

// c# int[,] m = new int[w, h]; // fill matrix m for (int i = 0; i < 2 * (w + h - 2); ++i) { int e = i < w - 1 ? m[i, 0] : i < w + h - 2 ? m[w - 1, i - w + 1] : i < 2 * w + h - 3 ? m[2 * w + h - 3 - i, h - 1] : m[0, 2 * (w + h - 2) - i]; // do something with element e }