Operasi matriks menggunakan fungsi

Posted on 15.07.2011

0



program operasi_matrix;
const
max = 50;
type
Mat = array [1..max, 1..max] of real;
var
u1, u2, u3, u4 : byte;
i, j, pilih    : byte;
m1, m2, m3     : Mat;

procedure tampilMatrix(a: Mat; m, n : byte);
var
i, j : byte;
begin
for i:=1 to m do begin
for j:=1 to n do
write(a[i,j]:10:2, ‘ ‘);
writeln;
end;
end;

function buatMatrix(a: Mat; m, n : byte): Mat;
var
i, j : byte;
begin
for i:=1 to m do
for j:=1 to n do begin
write(‘[‘, i, ‘, ‘, j, ‘] = ‘); readln(a[i, j]);
end;
buatMatrix:=a;
end;

function tambahMatrix(a, b : Mat; m, n : byte): Mat;
var
i, j : byte;
c : Mat;
begin
for i:=1 to m do
for j:=1 to n do
c[i, j]:=a[i, j] + b[i, j];
tambahMatrix:=c;
end;

function kurangMatrix(a, b : Mat; m, n : byte): Mat;
var
i, j : byte;
c : Mat;
begin
for i:=1 to m do
for j:=1 to n do
c[i, j]:=a[i, j] – b[i, j];
kurangMatrix:=c;
end;

function transposeMatrix(a : Mat; m, n : byte): Mat;
var
i, j : byte;
b : Mat;
begin
for i:=1 to m do
for j:=1 to n do
b[j, i]:=a[i, j];
transposeMatrix:=b;
end;

function kaliMatrix(a, b : Mat; m, n, o : byte): Mat;
var
i, j, k : byte;
c : Mat;
tmp : real;
begin
for i:=1 to m do begin
k:=1;
repeat
for j:=1 to n do
tmp:=tmp + (a[i, j] * b[j, k]);
c[i, k]:= tmp;
k := k+1;
tmp := 0;
until(k>o);
end;
kaliMatrix:=c;
end;

begin
writeln(‘=======================================’);
writeln(‘ OPERASI 2 MATRIKS’);
writeln(‘=======================================’);
writeln;
writeln(‘ 1. Penjumlahan’);
writeln(‘ 2. Pengurangan’);
writeln(‘ 3. Perkalian’);
writeln(‘ 4. Transpose’);
writeln;
write(‘Pilihan : ‘); readln(pilih);
case pilih of
1 : begin {operasi penjumlahan}
write(‘Ukuran matriks 1 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u1, u2);
write(‘Ukuran matriks 2 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u3, u4);
if (u1 = u3) and ( u2 = u4) then begin {syarat : dimensi harus sama}
m1:=buatMatrix(m1, u1, u2);
m2:=buatMatrix(m2, u1, u2);
m3:=tambahMatrix(m1, m2, u1, u2);

tampilMatrix(m3, u1, u2);
end;
end;
2 : begin {operasi pengurangan}
write(‘Ukuran matriks 1 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u1, u2);
write(‘Ukuran matriks 2 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u3, u4);
if (u1 = u3) and ( u2 = u4) then begin {syarat : dimensi harus sama}
m1:=buatMatrix(m1, u1, u2);
m2:=buatMatrix(m2, u1, u2);
m3:=kurangMatrix(m1, m2, u1, u2);

tampilMatrix(m3, u1, u2);
end;
end;
3 : begin {operasi perkalian}
write(‘Ukuran matriks 1 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u1, u2);
write(‘Ukuran matriks 2 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u3, u4);
if (u2 = u3) then begin {( baris x kolom) syarat : kolom matriks_1 = baris matriks_2}
m1:=buatMatrix(m1, u1, u2);
m2:=buatMatrix(m2, u2, u4);
m3:=kaliMatrix(m1, m2, u1, u2, u4);

tampilMatrix(m3, u1, u4);
end;
end;
4 : begin {operasi transpose}
write(‘Ukuran matriks 1 (max 50 x 50), contoh input : 3 4 –> ‘); readln(u1, u2);
m1:=buatMatrix(m1, u1, u2);
m2:=transposeMatrix(m1, u1, u2);

writeln(‘matriks : ‘);
tampilMatrix(m1, u1, u2);
writeln;
writeln(‘matriks setelah ditranspose: ‘);
tampilMatrix(m2, u2, u1);
end;
end;

{ readln; }
end.

Posted in: Pascal Code