Написать процедуру перемножения матрицы A(5, 10) на матрицу В(10, 4) - C#
Формулировка задачи:
помогите написать процедуры в С#
1)Написать процедуру перемножения матрицы A(5, 10) на матрицу В(10, 4).
Решение задачи: «Написать процедуру перемножения матрицы A(5, 10) на матрицу В(10, 4)»
textual
Листинг программы
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[,] A = new int[5, 10];
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
A[i, j] = rnd.Next(1,10);
}
}
int[,] B = new int[10, 4];
for (int i = 0; i < B.GetLength(0); i++)
{
for (int j = 0; j < B.GetLength(1); j++)
{
B[i, j] = rnd.Next(1,10);
}
}
Console.WriteLine("\nМатрица A:");
Print(A);
Console.WriteLine("\nМатрица B:");
Print(B);
Console.WriteLine("\nМатрица C = A * B:");
int[,] C = Multiplication(A, B);
Print(C);
Console.ReadLine();
}
static int[,] Multiplication(int[,] a, int[,] b)
{
if (a.GetLength(1) != b.GetLength(0)) throw new Exception("Матрицы нельзя перемножить");
int[,] r = new int[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
for (int k = 0; k < b.GetLength(0); k++)
{
r[i,j] += a[i,k] * b[k,j];
}
}
}
return r;
}
static void Print(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0} ", a[i, j]);
}
Console.WriteLine();
}
}
}
}