G
G
gridmaniac2017-05-01 14:48:11
Mathematics
gridmaniac, 2017-05-01 14:48:11

How to print the diagonals of a matrix not from left to right?

I solve the problem of finding sequences of identical balls on the diagonals.
There is a code for displaying / passing along the diagonals of the matrix from left to right, from top to bottom.

int size = 9; //Размер матрицы 9x9
string diagonal  = "";
for (int k = 0; k < size * 2; k++)
{
   for (int j = 0; j <= k; j++)
   {
      int i = k - j;
      if (i < size && j < size)
      {
         diagonal += mat[j, i] + " ";
      }
   }
   print(diagonal);
   diagonal = "";
}

Now I'm trying to draw the diagonals from top to bottom, from right to left, in order to view all the variants of the matrix diagonals for the presence of sequences of balls.
3ac8fdeae98145078d6d872e74edf359.png
Is it possible to modify this code for a mirror pass?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gridmaniac, 2017-05-01
@gridmaniac

My decision:

for (int k = size; k >= -size; k--)
{
  for (int j = 0; j < size - k; j++)
  {
    int i = k + j;
    if (i < size && j < size && i >= 0 && j >= 0)
    {
      diagonal += arr2[j, i] + " ";
    }
  }

  Console.WriteLine(diagonal);
  diagonal = "";
}

Working code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question