- Code: Select all
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Perkalian matriks A dan Matriks B");
int barisA, kolomA, barisB, kolomB;
string input;
Random randomInt = new Random();
Console.Write("Baris A =");
barisA = int.Parse(Console.ReadLine());
Console.Write("Kolom A dan Baris B =");
kolomA = barisB = int.Parse(Console.ReadLine());
Console.Write("Kolom B =");
kolomB = int.Parse(Console.ReadLine());
int[,] matA = new int[barisA, kolomA];
int[,] matB = new int[barisB, kolomB];
int[,] matC = new int[barisA, kolomB];
for (int i = 0; i < barisA; i++)
for (int j = 0; j < kolomA; j++)
matA[i, j] = randomInt.Next(1, 3);
for (int i = 0; i < barisB; i++)
for (int j = 0; j < kolomB; j++)
matB[i, j] = randomInt.Next(1, 3);
Console.Write("Matriks A : \n\n");
{
for (int i = 0; i < barisA; i++)
{
for (int j = 0; j < kolomA; j++)
Console.Write(matA[i, j] + "\t");
Console.WriteLine();
}
}
Console.Write("\n Matriks B :\n");
{
for (int i = 0; i < barisB; i++)
{
for (int j = 0; j < kolomB; j++)
Console.Write(matB[i, j] + "\t");
Console.WriteLine();
}
}
Console.WriteLine("\n Hasil perkalian matriks 2D A dan B :");
{
for (int a = 0; a < barisA; a++)
{
for (int b = 0; b < kolomB; b++)
{
matC[a, b] = 0;
for (int c = 0; c < kolomA; c++)
{
matC[a, b] += matA[a, c] * matB[c, b];
}
Console.Write(matC[a, b] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
semoga bisa berguna bagi yang membutuhkan
makasi
