D2 TEKNIK KOMPUTER (BSI PROGRAMMER)
ARRAY DALAM DELPHI
Array adalah sebuah variabel tunggal yang digunakan untuk menyimpan sekumpulan data. Dimana masing-masing data akan memiliki identitasnya tersendiri, berupa kedudukannya dalam kelompok. Data yang disimpan dalam sebuah array disebut dengan elemen dan keseluruhannya harus mempunyai komponen dalam jumlah yang tetap dan setiap komponen harus mempunyai tipe data yang sama (sejenis). Posisi masing-masing komponen dalam array dinyatakan sebagai nomor indeks. Indeks elemen adalah angka yang menyatakan urutan data yang menjadi anggota.
Contoh :
Var
NilUjian : array[1..5] of Real;
Contoh tersebut mempunyai arti : suatu deklarasi yang memberi tahu kompiler bahwa variabel yang bernama NilUjian merupakan suatu kumpulan variabel bertipe real sebanyak lima buah dengan indeks 1, 2, 3, 4, 5. Dengan masing-masing elemennya dapat dinyatakan sebagai NilUjian[1], NilUjian[2], NilUjian[3], NilUjian[4], dan NilUjian[5].
Contoh Program :
Berikut ini adalah contoh program aplikasi penanganan data yang disimpan dalam type data array, yaitu Penangana Data Alamat dengan tampilan seperti gambar di bawah ini :
Dengan Listing Program sebagai berikut :
procedure TForm1.Button1Click(Sender: TObject);
var s : string;
p,i : integer;
Nama : array[1..20] of string;
Alamat : array[1..30] of string;
begin
ListBox1.Items.Clear;
s := InputBox('Loop','Jumlah Data','');
p := StrToInt(s);
StringGrid1.Cells[0,0]:='Nama';
StringGrid1.Cells[1,0]:='Alamat';
for i:=1 to p do
begin
nama[i]:=InputBox('ID','Nama','');
Alamat[i]:=InputBox('ID','Alamat','');
ListBox1.Items.Add(IntToStr(i)+'.'+Nama[i]);
ListBox1.Items.Add(' '+Alamat[i]);
StringGrid1.Cells[0,i]:=Nama[i];
StringGrid1.Cells[1,i]:=Alamat[i];
end;
end;
Sumber : Pdf
Contoh Hasil Pemrograman Delphi Saya :
Code :
unit Abjad;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls , MMSystem;
type
TForm4 = class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
Timer1: TTimer;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
procedure TForm4.Button1Click(Sender: TObject);
begin
Form4.Enabled:=False;
if Edit1.Text = 'SAPI' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Sapi1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Sapi2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Sapi3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'KUDA' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kuda1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kuda2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kuda3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'IKAN' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ikan1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ikan2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ikan3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'KUCING' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kucing1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kucing2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kucing3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'AYAM' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ayam1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ayam2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ayam3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'BEBEK' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Bebek1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Bebek2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Bebek3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'ULAR' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ular1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ular2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ular3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'MACAN' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Macan1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Macan2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Macan3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'BURUNG' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Burung1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Burung2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Burung3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'RUSA' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Rusa1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Rusa2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Rusa3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else Exit;
end;
procedure TForm4.Button2Click(Sender: TObject);
Var Gambar : Array [1..10] Of String;
Var I:Integer;
begin
Gambar[1]:='Hewan_Sapi.jpg';
Gambar[2]:='Hewan_Kuda.jpg';
Gambar[3]:='Hewan_Ikan.jpg';
Gambar[4]:='Hewan_Kucing.jpg';
Gambar[5]:='Hewan_Ayam.jpg';
Gambar[6]:='Hewan_Bebek.jpg';
Gambar[7]:='Hewan_Ular.jpg';
Gambar[8]:='Hewan_Macan.jpg';
Gambar[9]:='Hewan_Burung.jpg';
Gambar[10]:='Hewan_Rusa.jpg';
I:=Random(10);
//-------------------------------------------------------//
//------------------------------------------------------//
if I <=0 then
Begin
I:=I+1;
End
Else If I =1 then
Begin
I:=I+1;
End
Else if I = 2 then
Begin
I:=I+1;
End
Else If I =3 then
Begin
I:=I+1;
End
Else If I =4 then
Begin
I:=I+1;
End
Else If I =5 then
Begin
I:=I+1;
End
Else If I =6 then
Begin
I:=I+1;
End
Else If I =7 then
Begin
I:=I+1;
End
Else If I =8 then
Begin
I:=I+1;
End
Else If I =9 then
Begin
I:=I+1;
End
Else If I = 10 then
Begin
I:=I-1;
End
Else Exit;
Image1.Picture.LoadFromFile(GetCurrentDir+'\Images\'+Gambar[I]);
//-------------------------------------------------------//
if I = 1 then
Begin
Edit1.Text:='SAPI';
End
Else if I = 2 then
Begin
Edit1.Text:='KUDA';
End
Else if I = 3 then
Begin
Edit1.Text:='IKAN';
End
Else if I = 4 then
Begin
Edit1.Text:='KUCING';
End
Else if I = 5 then
Begin
Edit1.Text:='AYAM';
End
Else if I = 6 then
Begin
Edit1.Text:='BEBEK';
End
Else if I = 7 then
Begin
Edit1.Text:='ULAR';
End
Else if I = 8 then
Begin
Edit1.Text:='MACAN';
End
Else if I = 9 then
Begin
Edit1.Text:='BURUNG';
End
Else if I = 10 then
Begin
Edit1.Text:='RUSA';
End
Else Exit;
//===============================================================//
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
Button2.Click;
end;
procedure TForm4.Timer1Timer(Sender: TObject);
begin
Label1.Left:=Label1.Left + 1;
if Label1.Left = Form4.Width then
Begin
Label1.Left:=1;
End;
end;
end.
Screenshut Program :
Sekian dan terima kasih delphier.........
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls , MMSystem;
type
TForm4 = class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
Timer1: TTimer;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
procedure TForm4.Button1Click(Sender: TObject);
begin
Form4.Enabled:=False;
if Edit1.Text = 'SAPI' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Sapi1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Sapi2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Sapi3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'KUDA' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kuda1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kuda2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kuda3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'IKAN' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ikan1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ikan2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ikan3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'KUCING' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kucing1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kucing2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Kucing3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'AYAM' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ayam1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ayam2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ayam3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'BEBEK' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Bebek1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Bebek2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Bebek3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'ULAR' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ular1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ular2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Ular3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'MACAN' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Macan1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Macan2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Macan3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'BURUNG' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Burung1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Burung2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Burung3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else if Edit1.Text = 'RUSA' then
Begin
PlaySound(pchar(GetCurrentDir+'\Sound_A\Rusa1.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Rusa2.wav'),0,SND_SYNC);
PlaySound(pchar(GetCurrentDir+'\Sound_A\Rusa3.wav'),0,SND_SYNC);
Form4.Enabled:=True;
Exit;
End
Else Exit;
end;
procedure TForm4.Button2Click(Sender: TObject);
Var Gambar : Array [1..10] Of String;
Var I:Integer;
begin
Gambar[1]:='Hewan_Sapi.jpg';
Gambar[2]:='Hewan_Kuda.jpg';
Gambar[3]:='Hewan_Ikan.jpg';
Gambar[4]:='Hewan_Kucing.jpg';
Gambar[5]:='Hewan_Ayam.jpg';
Gambar[6]:='Hewan_Bebek.jpg';
Gambar[7]:='Hewan_Ular.jpg';
Gambar[8]:='Hewan_Macan.jpg';
Gambar[9]:='Hewan_Burung.jpg';
Gambar[10]:='Hewan_Rusa.jpg';
I:=Random(10);
//-------------------------------------------------------//
//------------------------------------------------------//
if I <=0 then
Begin
I:=I+1;
End
Else If I =1 then
Begin
I:=I+1;
End
Else if I = 2 then
Begin
I:=I+1;
End
Else If I =3 then
Begin
I:=I+1;
End
Else If I =4 then
Begin
I:=I+1;
End
Else If I =5 then
Begin
I:=I+1;
End
Else If I =6 then
Begin
I:=I+1;
End
Else If I =7 then
Begin
I:=I+1;
End
Else If I =8 then
Begin
I:=I+1;
End
Else If I =9 then
Begin
I:=I+1;
End
Else If I = 10 then
Begin
I:=I-1;
End
Else Exit;
Image1.Picture.LoadFromFile(GetCurrentDir+'\Images\'+Gambar[I]);
//-------------------------------------------------------//
if I = 1 then
Begin
Edit1.Text:='SAPI';
End
Else if I = 2 then
Begin
Edit1.Text:='KUDA';
End
Else if I = 3 then
Begin
Edit1.Text:='IKAN';
End
Else if I = 4 then
Begin
Edit1.Text:='KUCING';
End
Else if I = 5 then
Begin
Edit1.Text:='AYAM';
End
Else if I = 6 then
Begin
Edit1.Text:='BEBEK';
End
Else if I = 7 then
Begin
Edit1.Text:='ULAR';
End
Else if I = 8 then
Begin
Edit1.Text:='MACAN';
End
Else if I = 9 then
Begin
Edit1.Text:='BURUNG';
End
Else if I = 10 then
Begin
Edit1.Text:='RUSA';
End
Else Exit;
//===============================================================//
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
Button2.Click;
end;
procedure TForm4.Timer1Timer(Sender: TObject);
begin
Label1.Left:=Label1.Left + 1;
if Label1.Left = Form4.Width then
Begin
Label1.Left:=1;
End;
end;
end.
Screenshut Program :
Sekian dan terima kasih delphier.........