-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
56 lines (45 loc) · 1.65 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Homework.ITAcademy5
{
class Program
{
static void Main(string[] args)
{
var firstMatrix = new Matrix();
firstMatrix.InitializingDimensions();
var secondMatrix = new Matrix();
secondMatrix.InitializingDimensions();
if (firstMatrix.NumberOfRows != secondMatrix.NumberOrColumns || firstMatrix.NumberOrColumns != secondMatrix.NumberOfRows)
{
Console.WriteLine("The number of rows in the first matrix is not equal to the number of columns in the second");
Environment.Exit(0);
}
else
{
var thirdMatrix = new Matrix { TheMatrix = MultiplyMatrix(firstMatrix, secondMatrix) };
Console.WriteLine("Result matrix");
thirdMatrix.Show();
}
}
public static int[,] MultiplyMatrix(Matrix first, Matrix second)
{
var resultMatrix = new int[first.NumberOfRows, second.NumberOrColumns];
var commonLength = first.NumberOrColumns;
Parallel.For(0, resultMatrix.GetLength(0), (i) =>
{
for (var j = 0; j < resultMatrix.GetLength(1); j++)
{
var nextVal = 0;
for (var k = 0; k < commonLength; k++)
{
nextVal += first.TheMatrix[i, k] * second.TheMatrix[k, j];
}
resultMatrix[i, j] = nextVal;
}
});
return resultMatrix;
}
}
}