Найти сумму элементов квадратной матрицы из заштрихованной области - C#
Формулировка задачи:
Доброе утро. Помогите найти ошибку, суть алгоритма понял, а довести до ума не получается
Задание: "Найти S - сумму элементов квадратной матрицы A из заштрихованной области." (см.вложение)
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
const int n = 4;
double S = 0;
int[,] y = new int[n, n] {
{ 2, 2, 8, 9 },
{ 4, 5, 6, 2 },
{ 7, 45, 1, 66 },
{ 1, 4, 5, 9 }
};
for (int row = 0; row < n; row++)
{
Console.Write(" ");
for (int col = 0; col < n; col++)
{
Console.Write(" ");
Console.Write(y[row, col]);
}
Console.Write("\n");
}
for (int row = 0; row < n / 2; row++)
{
for (int col = row; col < n - row; col++)
{
S += y[row, col];
Console.Write(y[row, col]);
}
}
for (int row = n / 2; row < n; row++)
{
for (int col = n - row; col < n; col++)
{
S += y[row, col];
Console.Write(y[row, col]);
Console.Write(" ");
}
}
Console.WriteLine(S);
Console.Write(" ");
Console.ReadLine();
}
}
}Решение задачи: «Найти сумму элементов квадратной матрицы из заштрихованной области»
textual
Листинг программы
for (int i = 0; i < y.GetLength(0); i++) for (int j = i; j < y.GetLength(1) - i; j++) S += y[i, j];