c# - How can I get the sum of numbers from a diagonal line? -


here sum diagonal top-left bottom-right:

public int sumardiagonal() {     int x = 0;     (int f = 0; f < filas; f++)     {         (int c = 0; c < columnas; c++)         {             if (f == c)             {                 x += m[f,c];             }         }     }     return x; } 

how can go top-right bottom-left?

your original code 2 nested loops not efficient. better this:

public int sumardiagonal()  {   int x = 0;   (int = 0; < math.min(filas,columnas); ++i)    x += m[i,i];   return x;  }   public int sumarantidiagonal()  {   int x = 0;   (int = 0; < math.min(filas,columnas); ++i)    x += m[filas - 1 - i,i];   return x;  }  

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -