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

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

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