program PenjumlahanPerkalianMatrik;
uses wincrt;
var
matriks1, matriks2, matriks_hasil1, matriks_hasil2: array[1..3, 1..3] of integer;
{diatas mrpkn pendeklarasian array 2 dimensi yg mempunyai 3 elemen yaitu 1,2,3}
i, j,k,l: integer;
begin
Randomize;
for i :=1 to 3 do {hanya menjalankan for saja}
for j:=1 to 3 do {melakukan 2 statement, makanya harus dikelompokkan dengan begin dan end}
begin
matriks1[i,j]:=random(5);
matriks2[i,j]:=random(5);
end;
writeln('Matriks 1');
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
write(matriks1[i,j]:5);
end;
writeln;
end;
writeln('Matriks 2');
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
write(matriks2[i,j]:5);
end;
writeln;
end;
for i:=1 to 3 do
for j:=1 to 3 do
begin
l:=0;
for k:=1 to 3 do
l:=l + matriks1[i,k] * matriks2[k,j];
matriks_hasil1[i,j]:=l;
matriks_hasil2[i,j]:=matriks1[i,j]+matriks2[i,j];
end;
writeln('Hasil perkalian matriks 1 dan matriks 2');
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
write(matriks_hasil1[i,j]:5);
end;
writeln;
end;
writeln('Hasil penjumlahan matriks 1 dan matriks 2');
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
write(matriks_hasil2[i,j]:5);
end;
writeln;
end;
end.
Tidak ada komentar: