-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (55 loc) · 1.47 KB
/
Program.cs
File metadata and controls
66 lines (55 loc) · 1.47 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
using System;
class Program
{
static void Main()
{
int[,] matriksA = {
{1, 2},
{3, 4}
};
int[,] matriksB = {
{5, 6},
{7, 8}
};
int[,] hasil = PerkalianMatriks(matriksA, matriksB);
Console.WriteLine("Hasil Perkalian Matriks:");
CetakMatriks(hasil);
}
static int[,] PerkalianMatriks(int[,] A, int[,] B)
{
int barisA = A.GetLength(0);
int kolomA = A.GetLength(1);
int barisB = B.GetLength(0);
int kolomB = B.GetLength(1);
if (kolomA != barisB)
{
throw new InvalidOperationException("Jumlah kolom matriks A harus sama dengan jumlah baris matriks B");
}
int[,] hasil = new int[barisA, kolomB];
for (int i = 0; i < barisA; i++)
{
for (int j = 0; j < kolomB; j++)
{
hasil[i, j] = 0;
for (int k = 0; k < kolomA; k++)
{
hasil[i, j] += A[i, k] * B[k, j];
}
}
}
return hasil;
}
static void CetakMatriks(int[,] matriks)
{
int baris = matriks.GetLength(0);
int kolom = matriks.GetLength(1);
for (int i = 0; i < baris; i++)
{
for (int j = 0; j < kolom; j++)
{
Console.Write(matriks[i, j] + " ");
}
Console.WriteLine();
}
}
}