【发布时间】:2014-03-06 23:58:26
【问题描述】:
这是到目前为止的全部代码。我只是无法让这一行运行。我需要“并行”线的帮助。这是针对需要多线程的矩阵乘法问题。程序运行直到输出然后它会爆炸。请帮忙!
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace matrix_multiplication
{
class Program
{
static void Main(string[] args)
{
int i, j,m,n; //This is where you tell the PC you want a number and store the var//
Console.WriteLine("Enter the number of rows (must be >1) you want in the MATRIX:"); //This writes to the screen//
m = Convert.ToInt32(Console.ReadLine()); //This converts the input to a number//
Console.WriteLine("Enter the number of columns (must be >1) you want in the MATRIX:");
n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[m, n];
Console.WriteLine("Enter the numbers for the first MATRIX:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("The first MATRIX is:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j]+"\t");
}
Console.WriteLine();
}
int[,] b = new int[m, n];
Console.WriteLine("Enter the numbers for the second MATRIX:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("The second MATRIX is:");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(b[i, j]+"\t");
}
Console.WriteLine();
}
Console.WriteLine("The MATRIX multiplication is:");
int[,] c = new int[m, n];
**Parallel.For(0, m, =>**
{
for (j = 0; j < n; j++)
{
c[i, j] = 0;
for (int k = 0; k < 2; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
});
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j]+"\t");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
【问题讨论】:
-
哪一行给出了错误?
标签: c# multithreading loops matrix