Beranda > Uncategorized > FUNGSI ARRAY DALAM ALGORITMA

FUNGSI ARRAY DALAM ALGORITMA

Sekarang berbagi ilmu akan membahas tentang FUNGSI ARRAY DALAM ALGORITMA

Array adalah tipe data tersetruktur yang mempunyai komponen-komponen yang mempunyai tipe sama. komponen-komponen tersebut sebagai komponen type larik mempunyai yang komponennya tetep. array dapat bertype sederhana seperti BYTE,WORD,INTERGER,REAL,BOLEAN,CHAR,STRING dan type data SCALAR/SUBRAGE

CONTOH PROGRAM ARRAY

Program Contoh_Array_Input;
Uses Crt;
Var
Bilangan : array[1..50] of Integer;
Begin
ClrScr;
Bilangan[1]:=3;
Bilangan[2]:=29;
Bilangan[3]:=30;
Bilangan[4]:=31;
Bilangan[5]:=23;
Writeln(‘nilai varibel bilangan ke 3 =’,Bilangan[3]);
Readln;
End.

============================

Program Contoh_Deklarasi_Array_Beragam;
Uses Crt;
Var
NPM : array[1..20] of string[10];
Nama : array[1..20] of string[25];
Nilai : array[1..20] of real;
Umur : array[1..20] of byte;
banyak,i : integer;
Begin
ClrScr;
Write(‘Isi berapa data array yang diperlukan :’);Readln(banyak);
For i := 1 to banyak Do
Begin
Write(‘NPM =’);Readln(NPM[i]);
Write(‘Nama =’);readln(Nama[i]);
Write(‘Nilai=’);readln(Nilai[i]);
Write(‘Umur =’);readln(Umur[i]);
End;
{cetak varibel array}
Writeln(‘NPM NAMA NILAI UMUR ‘);
For i:= 1 to banyak Do
Begin
Writeln(Npm[i]:10,Nama[i]:25,Nilai[i]:3:2,’ ‘,Umur[i]:3);
End;
Readln;
End.

================================================
Var
Nilai : Array[1..10] of Integer;
pada contoh ini array nilai mempunyai 10 buah elemen yaitu dari 1 sampai 10. Array tersebut dapat dideklarasikan dengan type seperti berikut ini :

Type
Skala = 1..10;
Var
Nilai : Array [skala] of Integer;
atau :
Type
Skala = 1..10;
Y = Array[skala] of Integer;
Var
Nilai : Y;
atau :
Type
Y = Array[1..10] of Integer;
Var
Nilai : Y;
atau :
Const
Atas =1;
Bawah = 5;
type
Y = Array[Atas..Bawah] of Integer;
Var
Nilai : Y;

=====================================================

Program Contoh_Deklarasi_Array_Konstan;
Uses Crt;
Const
Tetap : Array[1..4] of Integer=(7,10,21,20);
Var
i : Integer;
Begin
For i:= 1 to 4 Do
Writeln(‘Nilai Konstan array ke ‘,i:2,’ =’,Tetap[i]);
Readln;
End.

==============================================
Program Constanta_Array_String;
Uses Crt;
Type
A = Array [1..5] of String;
Const
Nama : A = (‘basic’,’pascal’,’cobol’,’paradox’,’dbase’);
Var
i : Integer;
Begin
For i:= 1 to 5 Do
Writeln(‘Nilai Array ke-‘,i:2,’= ‘,Nama[i]);
readln;
end.
==================================
Program String_Adalah_Array_Tipe_Char;
Uses Crt;
Var
Nama : string;
i : Integer;
Begin
Nama:=’Turbo Pascal’;
For i:= 1 to Length(nama) Do
Writeln(‘Elemen ‘,i,’ dari ‘,Nama,’= ‘,Nama[i]);
Readln;
End.

=========================================
Program Mencari_Bilangan_Prima_Dengan_Array;
Uses Crt;
Var
Prima : Array[1..100] of Integer;
i,j : Integer;
bil : Integer;
Begin
ClrScr;
For i := 2 to 100 Do
Begin
Prima[i]:=i;
For j:= 2 to i-1 Do
Begin
bil := (i mod j); {* i dibagi j dicek apakah 0*}
If bil = 0 then Prima[i]:=0; {*jika habis dibagi,berarti bkn prima*}
End;
If Prima[i]<> 0 Then Write(Prima[i],’ ‘); {*cetak array yg prima*}
End;
Readln;
End.

================================================

Program Penggunaan_Array_Untuk_Sortir_Buble_Sort;
Uses Crt;
Var
nil1 : Array[1..100] of Integer;
n,i,j,dum : Integer;
Begin
ClrScr;
Write(‘mau isi berapa data acak (integer) =’); readln(n);
For i := 1 to n Do
Begin
Write(‘Data Ke ‘,i,’:’);Readln(nil1[i]);
End;
{* penyapuan proses}
for i:= 1 to n-1 do
begin
for j:= i to n do
begin
if nil1[j]
begin
dum:=nil1[j];
nil1[j]:=nil1[i];
nil1[i]:=dum;
end;
end;
end;
writeln;
writeln(‘Hasil Sortir’);
for i := 1 to n do
write(nil1[i]:3);
readln;
end.

=================================================

Program Deklarasi_Array_Dua_Dimensi;
Uses Crt;
Var Tabel : Array[1..3,1..2] of Integer;
i,j : Integer;
Begin
ClrScr;
Tabel[1,1]:=1;
Tabel[1,2]:=2;
Tabel[2,1]:=3;
Tabel[2,2]:=4;
Tabel[3,1]:=5;
Tabel[3,2]:=6;
For I := 1 to 3 Do Begin For J:= 1 to 2 Do Begin Writeln(‘Elemen ‘,i,’,’,j,’= ‘,tabel[i,j]);
End;
End;
Readln;
End.

=====================================================
Program Penjumlahan_Matrik;
Uses Crt;
Var
Matrik1,Matrik2, Hasil : Array[1..3,1..2] of Integer;
i,j : Integer;
Begin
ClrScr;
{ input matrik ke satu }
Writeln(‘ Elemen matrik satu’);
For i := 1 to 3 Do
Begin
For j := 1 to 2 Do
Begin
Write(‘Elemen baris -‘,i,’ Kolom -‘,j,’= ‘);
Readln(matrik1[i,j]);
End;
End;
{input matrik ke dua}
Writeln(‘input elemen matrik dua’);
For i:= 1 to 3 Do
Begin
For j:= 1 to 2 Do
Begin
Write(‘Elemen baris -‘,i,’ kolom -‘,j,’= ‘);
Readln(matrik2[i,j]);
End;
End;
{proses penjumlahan tiap elemen}
For i := 1 to 3 Do
Begin
For j:= 1 to 2 Do
Begin
Hasil[i,j]:=Matrik1[i,j]+Matrik2[i,j];
End;
End;
{proses cetak hasil}
For i:= 1 to 3 Do
Begin
For j:= 1 to 2 Do
Begin
Write(Hasil[i,j]:6);
End;
Writeln;
End;
Readln;
End.

===============================================

Program Contoh_Pengiriman_Parameter_Array_Di_Procedure;
Uses Crt;
Const
Garis =’—————————————————‘;
Type
Untai = Array[1..10] of String[15];
Bulat = Array[1..10] of Integer;
Huruf = Array[1..10] of Char;
Var
i,Banyak : Integer;
Procedure Proses(Nama:Untai;Nilai:Bulat);
Var
Ket : String;
Abjad : Char;
Begin
Writeln(Garis);
Writeln(‘Nama Nilai Abjad Keterangan’);
Writeln(Garis);
For i := 1 to Banyak Do
Begin
If Nilai[i] > 90 Then
Begin
Abjad:=’A’;
Ket :=’Istimewa’;
End;
If (Nilai[i]<90) And (Nilai[i]>70) Then
Begin
Abjad:=’B’;
Ket :=’Memuaskan’;
End;
If (Nilai[i]<70) And (Nilai[i]>60) Then
Begin
Abjad:=’C’;
Ket :=’Cukup’;
End;
If (Nilai[i]<60) And (Nilai[i]>45) Then
Begin
Abjad:=’D’;
Ket :=’Kurang’;
End;
If Nilai[i]< 45 Then
Begin
Abjad:=’E’;
Ket :=’Sangat kurang’;
End;
Writeln(Nama[i]:15,’ ‘,Nilai[i]:4,’ ‘,Abjad,’ ‘,Ket:15);
End;
Writeln(Garis);
End;
Procedure Masuk_Data;
Var
Nama : Untai;
Nilai : Bulat;
Begin
Write(‘Banyak data =’);Readln(Banyak);
For i:= 1 to Banyak Do
Begin
ClrScr;
Writeln(‘Data ke – ‘,i);
Write(‘Nama =’);readln(Nama[i]);
Write(‘Nilai =’);readln(Nilai[i]);
End;
Proses(Nama,Nilai);
End;
{modul Utama}
Begin
Masuk_Data;
Readln;
End.

JIKA SUDAH LANGSUNG SAJA DI RUNNING GENGAN MENEKAN CTRL+F9

CUKUP SEKIAN BERBAGI ILMU DARI SAYA MUDAH-MUDAHAN BISA MEMBANTU ANDA DALAM PEMBELAJARAN ALGORIMA

TERIMA KASIH ATAS K

Kategori:Uncategorized
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: