Класс Parallel, ошибка IndexOutOfRangeException - C#
Формулировка задачи:
Здравствуйте. Не могу понять в чем дело, выдает ошибку IndexOutOfRangeException на строчке ress[i] += Math.Pow(a[i, j], 2);
Помоги разобраться.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace arch3
{
class Program
{
static void Main(string[] args)
{
int i, j;
const int n = 5, m = 5;
double[,] a = new double[m, n];
double[] ress = new double[m];
double result = 1;
Stopwatch watch = new Stopwatch();
watch.Start();
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i, j] = i + j;
Parallel.For(0, n - 1, delegate(int k)
{
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write("{0,4}", a[i, j]);
ress[i] += Math.Pow(a[i, j], 2);
}
Console.WriteLine();
Console.WriteLine("Сумма по столбцам равна: " + ress[i]);
result *= ress[i];
}
}
);
Console.WriteLine("Произведение по строкам равно: " + result);
watch.Stop();
Console.WriteLine("Время выполнения матрицы " + m + "*" + n + " Затраченное время:" + watch.Elapsed.Milliseconds + "мсек.\r\n");
Console.WriteLine("Время выполнения матрицы " + m + "*" + n + " Затраченное время:" + watch.Elapsed + "такт.\r\n");
Console.ReadKey();
}
}
}Решение задачи: «Класс Parallel, ошибка IndexOutOfRangeException»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ConsoleApplication17 {
class Program {
static void Main(string[] args) {
int i, j;
const int n = 5, m = 5;
double[,] a = new double[m, n];
double[] ress = new double[m];
double result = 1;
Stopwatch watch = new Stopwatch();
watch.Start();
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i, j] = i + j;
Parallel.For(0, n - 1, delegate(int k) {
for (i = 0; i < m; i++) {
int index1 = i;
for (j = 0; j < n; j++) {
int index2 = j;
Console.Write("{0,4}", a[index1, index2]);
ress[index1] += Math.Pow(a[index1, index2], 2);
}
Console.WriteLine();
Console.WriteLine("Сумма по столбцам равна: " + ress[index1]);
result *= ress[index1];
}
}
);
Console.WriteLine("Произведение по строкам равно: " + result);
watch.Stop();
Console.WriteLine("Время выполнения матрицы " + m + "*" + n + " Затраченное время:" + watch.Elapsed.Milliseconds + "мсек.\r\n");
Console.WriteLine("Время выполнения матрицы " + m + "*" + n + " Затраченное время:" + watch.Elapsed + "такт.\r\n");
Console.ReadKey();
}
}
}