Script membuat perkalian matriks menggunakan pascal:
program matriks_soal1_raffi_111221110;
uses WinCrt;
var
Ma,Mb,Mr:array[1..10,1..10] of integer;
bar1,bar2,kol1,kol2,x1,x2,y1,y2,n:integer;
t,t1,t2,x,y,z:shortint;
c:char;
begin
y:=80;
gotoxy (1,1); for x:= 1 to y do write ('=');
gotoxy (1,3); for x:= 1 to y do write ('=');
gotoxy (1,5); for x:= 1 to y do write ('=');
gotoxy (1,8); for x:= 1 to y do write ('=');
gotoxy (40,4); write ('|');
gotoxy (40,6); write ('|');
gotoxy (40,7); write ('|');
gotoxy (31,2); write ('PERKALIAN 2 MATRIKS');
gotoxy (15,4); write ('Matriks A');
gotoxy (57,4); write ('Matriks B');
gotoxy (7,6); write ('Jumlah Baris Matriks A : '); readln(bar1);
gotoxy (7,7); write ('Jumlah Kolom Matriks A : '); readln(kol1);
gotoxy (47,6); write ('Jumlah Baris Matriks B : '); readln (bar2);
gotoxy (47,7); write ('Jumlah Baris Matriks B : '); readln (kol2);
if kol1=bar2 then
begin
t1:=bar1*kol1;
t2:=bar2*kol2;
if t1>t2 then t:=t1 else t:=t2;
for x:= 1 to t do
begin
gotoxy (40,8+x); write ('|');
end;
gotoxy (1,9+t); for x:=1 to y do write ('='); writeln;
for x1:= 1 to bar1 do
begin
for y1:= 1 to kol1 do
begin
z:=z+1;
gotoxy (4,8+z);
write('Masukkan Data Matriks A [',x1,':',y1,'] : '); readln(Ma[x1,y1]);
end;
end;
z:=0;
for x2:= 1 to bar2 do
begin
for y2:= 1 to kol2 do
begin
z:=z+1;
gotoxy (44,8+z);
write ('Masukkan Data Matriks B [',x2,':',y2,'] : '); readln(Mb[x2,y2]);
end;
end;
gotoxy (27,10+t1+t2); write ('Press any key to continue...'); readkey;
clrscr;
gotoxy (1,1); for x:= 1 to y do write ('=');
gotoxy (1,3); for x:= 1 to y do write ('=');
gotoxy (20,2); write ('Hasil Perkalian Matriks A * Matriks B');
writeln;
for x1:= 1 to bar1 do
begin
gotoxy (33,5+x1); write ('|');
for n:= 1 to kol2 do
begin
for y1:= 1 to kol1 do
begin
for y2:= n to kol2 do
begin
for x2:=y1 to bar2 do
begin
Mr[x1,y2]:=Mr[x1,y2]+Ma[x1,y1]*Mb[x2,y2];
if kol1=y1 then
begin
write(Mr[x1,y2]:3);
end;
x2:=bar2;
end;
y2:=kol2;
end;
end;
end;
write ('|':3);
writeln;
end;
writeln;
end else
begin
writeln;
gotoxy (1,14); for x:=1 to y do write ('=');
gotoxy (1,21); for x:=1 to y do write ('=');
gotoxy (1,16); for x:=1 to y do write ('=');
gotoxy (35,15); writeln ('!! ERROR !!');
gotoxy (8,18); writeln('Operasi Perkalian dari Matriks A * Matriks B tidak dapat dilakukan');
gotoxy (3,19); writeln('karena kolom Matriks A (',kol1,' Kolom) tidak sama dengan baris Matriks B (',bar2,' baris)');
end;
end.
Berikut ini adalah contoh gambar hasil program yg dijalankan :
Jika kolom matriks A tidak sama dengan baris matriks B, tampilan yang akan keluar seperti gambar berikut :
No comments:
Post a Comment