using System;
public class Test
{
public static void MultiplyMatrix()
{
int[,] a = new int[5, 5];
int[,] b = new int[5, 5];
int[,] c = new int[5, 5];
Random ran = new Random();
// Инициализируем данный массив
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
a[i, j] = ran.Next(1, 15);
b[i, j] = ran.Next(1, 15);
c[i, j] = ran.Next(1, 15);
}
Console.WriteLine();
}
if (a.GetLength(1) == b.GetLength(0))
{
c = new int[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < c.GetLength(0); i++)
{
for (int j = 0; j < c.GetLength(1); j++)
{
c[i, j] = 0;
for (int k = 0; k < a.GetLength(1); k++) // OR k<b.GetLength(0)
c[i, j] = c[i, j] + a[i, k] * b[k, j];
}
}
}
else
{
Console.WriteLine(c[1, 1]);
Console.WriteLine("\n Number of columns in First Matrix should be equal to Number of rows in Second Matrix.");
Console.WriteLine("\n Please re-enter correct dimensions.");
Environment.Exit(-1);
}
}
public static void Main()
{
MultiplyMatrix ();
}
}
dXNpbmcgU3lzdGVtOwoKcHVibGljIGNsYXNzIFRlc3QKewoJCgkKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgTXVsdGlwbHlNYXRyaXgoKQogICAgewogICAgCWludFssXSBhID0gbmV3IGludFs1LCA1XTsKICAgIAlpbnRbLF0gYiA9IG5ldyBpbnRbNSwgNV07CiAgICAJaW50WyxdIGMgPSBuZXcgaW50WzUsIDVdOwogICAgCQogICAgCVJhbmRvbSByYW4gPSBuZXcgUmFuZG9tKCk7CgovLyDQmNC90LjRhtC40LDQu9C40LfQuNGA0YPQtdC8INC00LDQvdC90YvQuSDQvNCw0YHRgdC40LIKZm9yIChpbnQgaSA9IDA7IGkgPCA0OyBpKyspCnsKICAgIGZvciAoaW50IGogPSAwOyBqIDwgNTsgaisrKQogICAgewogICAgICAgYVtpLCBqXSA9IHJhbi5OZXh0KDEsIDE1KTsKICAgICAgIGJbaSwgal0gPSByYW4uTmV4dCgxLCAxNSk7CiAgICAgICBjW2ksIGpdID0gcmFuLk5leHQoMSwgMTUpOwogICAgfQogICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKfQogICAgCQogICAgICAgIGlmIChhLkdldExlbmd0aCgxKSA9PSBiLkdldExlbmd0aCgwKSkKICAgICAgICB7CiAgICAgICAgICAgIGMgPSBuZXcgaW50W2EuR2V0TGVuZ3RoKDApLCBiLkdldExlbmd0aCgxKV07CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgYy5HZXRMZW5ndGgoMCk7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBjLkdldExlbmd0aCgxKTsgaisrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGNbaSwgal0gPSAwOwogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgYS5HZXRMZW5ndGgoMSk7IGsrKykgLy8gT1IgazxiLkdldExlbmd0aCgwKQogICAgICAgICAgICAgICAgICAgICAgICBjW2ksIGpdID0gY1tpLCBqXSArIGFbaSwga10gKiBiW2ssIGpdOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgCUNvbnNvbGUuV3JpdGVMaW5lKGNbMSwgMV0pOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXG4gTnVtYmVyIG9mIGNvbHVtbnMgaW4gRmlyc3QgTWF0cml4IHNob3VsZCBiZSBlcXVhbCB0byBOdW1iZXIgb2Ygcm93cyBpbiBTZWNvbmQgTWF0cml4LiIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXG4gUGxlYXNlIHJlLWVudGVyIGNvcnJlY3QgZGltZW5zaW9ucy4iKTsKICAgICAgICAgICAgRW52aXJvbm1lbnQuRXhpdCgtMSk7CiAgICAgICAgfQogICAgfQogICAgCglwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCgl7CgkJCgkJTXVsdGlwbHlNYXRyaXggKCk7Cgl9Cn0=