Kamis, 15 Desember 2011

Contoh Program Matrik

CONTOH PROGRAM BENTUK MATRIK

program matriks;
uses wincrt;
var
x:array[1..100,1..100] of integer;
p,l,i,j:integer;
begin
write('banyaknya kolom = '); read(p);
write('banyaknya baris = '); read(l);
 writeln('tulis kebawah sebanyak baris kali kolom');
for i:=1 to l do
for j:=1 to p do
readln(x[i,j]);
writeln('bentuk matriknya : ');
for i:=1 to l do
begin
for j:=1 to p do
write(x[i,j],' ');
writeln;
end;
end.



CONTOH PROGRAM PENJUMLAHAN DAN PENGURANGAN MATRIK
program matrix;
uses wincrt;
type
    larik=array[1..25,1..25] of real;
var
   i,j,k:byte;
   bar,col:byte;
   a,b,c,d,e:larik;
begin
     write ('baris matrik ?  ');readln(bar);
     write ('kolom matrik ? ');readln(col);
  
     writeln;

     writeln ('matrik pertama');
     for i:=1 to bar do
     begin
          for j:=1 to col do
          begin
               write ('nilai[',i,',',j,']');readln(a[i,j]);
          end;
          writeln;
     end;
     writeln;

     writeln ('matrik kedua');
     writeln;
   
     for i:=1 to bar do
     begin
          for j:=1 to col do
          begin
               write ('nilai[',i,',',j,']');readln(b[i,j]);
          end;
          writeln;
     end;

      writeln;

   
      writeln;
     for i:=1 to bar do
     begin
          for j:=1 to bar do
          begin
               for k:=1 to col do
                   d[i,j]:=a[i,j]+b[i,j]
               end;
          end;

     writeln;
     for i:=1 to bar do
     begin
          for j:=1 to bar do
          begin
               for k:=1 to col do
                   e[i,j]:=a[i,j]-b[i,j]
               end;
          end;
     clrscr;
      writeln ('matrik pertama:');
      for i:=1 to bar do
     begin
          for j:=1 to col do
          begin
               write (a[i,j]:0:0,' ');
          end;
     writeln;
     end;

     writeln ('matrik kedua:');

      for i:=1 to bar do
     begin
          for j:=1 to col do
          begin
               write (b[i,j]:0:0,' ');
          end;
     writeln;
     end;


     writeln ('hasil penjumlahan matrik:');
     writeln;
     for i:=1 to bar do
     begin
          for j:=1 to col do
          write(d[i,j]:9:2);
          writeln;
     end;

     writeln ('hasil pengurangan matrik:');
     writeln;
     for i:=1 to bar do
     begin
          for j:=1 to col do
          write(e[i,j]:9:2);
          writeln;
     end;
end.



CONTOH PROGRAM PERKALIAN MATRIK
program matrik_penjumlahan;
uses wincrt;
var a,b,c: array[1..10,1..10] of integer;
i,j,k1,k2,b1,b2,sum,r:integer;

... begin
write('Masukkan baris Matrik A = ');readln(b1);
write('Masukkan kolom Matrik A = ');readln(k1);
write('Masukkan baris Matrik B = ');readln(b2);
write('Masukkan kolom Matrik B = ');readln(k2);
for i:=1 to b1 do
for j:=1 to k1 do
readln(a[i,j]);
writeln;
for i:=1 to b2 do
for j:=1 to k2 do
readln(b[i,j]);
writeln;
writeln('Matrik A');
writeln('==================');
for i:=1 to b1 do
begin
for j:=1 to k1 do
write(a[i,j],' ');
writeln;
end;
writeln;
writeln('Matrik B');
writeln('===================');
for i:=1 to b2 do
begin
for j:=1 to k2 do
write(b[i,j],' ');
writeln;

end;
writeln;
writeln('Matrik A*B');
writeln('================');
for i:=1 to b1 do
begin
for j:=1 to k2 do
begin
sum:=0;
for r:=1 to k1 do
sum:=sum+a[i,r]*b[r,j];
c[i,j]:=sum;
write(c[i,j]:2,' ');
end;
writeln;
end;
end.



CONTOH PROGRAM DETERMINAN MATRIK
program determinan;
uses wincrt;
var A : array[1..2,1..2] of integer;
j,k,D : integer;
begin
For j:=1 to 2 do
begin
write('Entrikan baris ',j,' matriks A : ');
readln(A[j,1] , A[j,2]);
end;
For j:=1 to 2 do
begin
For k:=1 to 2 do write(A[j,k] : 5);
writeln;
end;
writeln;writeln;writeln;
D := A[1,1]*A[2,2] - A[1,2]*A[2,1];
writeln('Det(A) = ',D);
end.



CONTOH PROGRAM INVERSE MATRIK
program determinan;
uses wincrt;
var A : array[1..2,1..2] of integer;
j,k,D : integer;
begin
For j:=1 to 2 do
begin
write('Entrikan baris ',j,' matriks A : ');
readln(A[j,1] , A[j,2]);
end;
For j:=1 to 2 do
begin
For k:=1 to 2 do write(A[j,k] : 5);
writeln;
end;
writeln;writeln;writeln;
D := A[1,1]*A[2,2] - A[1,2]*A[2,1];
writeln('Det(A) = ',D);
end.

Tidak ada komentar:

Poskan Komentar

 
;