Tampilkan postingan dengan label Belajar. Tampilkan semua postingan
Tampilkan postingan dengan label Belajar. Tampilkan semua postingan

Senin, 23 November 2009

ARRAY

Pagi ni aku mosting lagi..........
sory bwat yang nggak bisa inggris, kmaren aku sosk inggris2 :D padahal aku juga nggak bisa bahasa inggris lho...........
Kali ni aku bkal bahas tentang array

menurut saya array adalah tipe data terstruktur yang terdiri dari sejumlah komponen-komponen yang mempunyai tipe sama. Komponen-komponen tersebut disebut sebagai komponen type, larik mempunyai jumlah komponen yang jumlahnya tetap. Banyaknya komponen dalam larik ditunjukkan oleh suatu index, dimana tiap komponen di array dapat diakses dengan menunjukkan nilai indexnya atau subskript. Array dapat bertipe data sederhana seperti byte, word, integer, real, bolean, char, string dan tipe data scalar atau subrange. Tipe larik mengartikan isi dari larik atau komponen- komponenya mempunyai nilai dengan tipe data tersebut.
Contoh:
Var
Untai : array[1..50] of Integer;
Pada contoh Array dengan nama untai telah dideklarasikan dengan tipe integer, dengan jumlah elemen maksimum 50 elemen, nilai dari elemen array tersebut diatas harus bertipe integer.

Contoh Program :
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.

Array juga dapat dideklarasikan bersama dengan tipe yang beragam seperti contoh dibawah ini :
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.

Untuk deklarasi array dapat digunakan beberapa cara seperti berikut ini :
Type
Angka = String[20];
Var
Nama : Array [1..50] of Angka;
Begin
.
.
End.

Deklarasi tipe indeks subrange integer Indeks pada array dapat tipe skalar atau subrange, tetapi tidak bisa real.
Contoh:
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;

I. Deklarasi Type Indeks Skalar
Indeks dari larik dapat berupa tipe skalar.
Contoh. :
Program Deklarasi_Indeks_Array_Skalar;
Uses Crt;
Var
Jum : Array[(jan,feb,mar,apr,mei)] of Integer;
Begin
Jum[jan]:=25;
Jum[feb]:=45;
Jum[mar]:=21;
Jum[apr]:=23;
Jum[mei]:=50;
Writeln('Jumlah nilai bulan maret =',Jum[mar]);
Readln;
End.
dapat juga ditulis :
type
Bln = (jan,feb,mar,apr,mei);
Var
Jum : Array[bln] of Integer;
atau :
type
Bln =(jan,feb,mar,apr,mei);
Var
Jum : Array[jan..mei] of Integer;

II. Deklarasi Konstanta Array
Array tidak hanya dapat berupa suatu varibel yang dideklarasikan di bagian deklarasi variabel, tetapi dapat juga berupa konstanta (const).
Contoh Program :
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.
konstanta array dapat juga berupa ketetapan dalam bentuk karakter seperti berikut.
Contoh Program :
Program Contoh_Konstan_Array_Char_;
Uses Crt;
Const
Huruf : Array[0..5] of Char=('A','B','C','D','E','F');
Var
i : Integer;
Begin
For i:= 0 to 5 Do
Writeln('Nilai konstan array ke',i:2,' = ',Huruf[i]);
Readln;
End.
Konstanta array dapat juga berupa string seperti berikut ini.
Contoh Program :
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.

Dalam pascal string merupakan array dari elemen- elemen karakter seperti berikut :
Contoh Program :
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.

contoh program bilangan prima dengan menggunakan bantuan array.
Contoh program :
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.

Contoh pengurutan data dengan metode buble sort, yaitu dengan cara penukaran, dapat dilihat pada contoh dibawah ini :
Contoh Program :
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.


III. Array Dua Dimensi
Di dalam pascal Array dapat berdimensi lebih dari satu yang disebut dengan array dimensi banyak (Multidimensional array), disini akan dibahas array 2 dimensi saja. Array 2 dimensi dapat mewakili suatu bentuk tabel atau matrik, yaitu indeks pertama menunjukkan baris dan indeks ke dua menunjukkan kolom dari tabel atau matrik.
1 2
1 2 3
Untuk mengetahui cara mendeklarasikan dari penggunaan aray dua dimensi dapat dilihat pada listing program dibawah ini .

Contoh Program:
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.


IV. Alternatif Deklarasi Array Dua Dimensi.
Ada beberapa cara dalam mendeklarasikan array dua dimensi, beberapa cara tersebut dapat dilihat dibawah ini :
Contoh :
Var
Tabel : Array[1..3] of Array[1..2] of Byte;
atau :
Type
Matrik = Array[1..3,1..2] of Byte;
Var
Tabel : Matrik;
atau :
Type
Baris = 1..3;
Kolom = 1..2;
Matrik = Array[Baris,Kolom] of Byte;
Var
Tabel : Matrik;
atau :
Type
Baris = 1..3;
Kolom=1..2;
Matrik= Array[Baris] of Array[Kolom] of Byte;
Var
Tabel : Matrik;
Dibawah ini akan diberikan listing program penggunaan array dua dimensi dalam aplikasi penjumlahan matrik :
Contoh Prorgam:
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.


V. Array Sebagai Parameter
Array dapat digunakan sebagai parameter yang dikirimkan baik secara nilai (by value) atau secara acuan (by reference) ke procedure atau ke function. Procedure yang menggunakan parameter berupa array harus dideklarasikan di dalam judul procedure yang menyebutkan parameternya bertipe array.

Contoh Program :
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.

Sabtu, 03 Oktober 2009

Introduction to Pascal

Introduction to Pascal

Introduction to Pascal
A Brief History of Pascal
• Designed by Prof.. Nicklaus Wirth from the Technical
University of Zurich, Switzerland in 1971.
• The name comes from Pascal Blaise Pascal, a specialist
mathematics and philosopi from France (17th century).
• Development of language Algol 60 and Algol W
(derivative of Algol 60).
• Have multiple versions, such as: Turbo Pascal, Ms.
Pascal (Microsoft), Apple Pascal, UCSD (University of
California at San Diego Pascal), etc..
• Turbo Pascal made by Borland Inc.. is version
The most widely used because it uses
Compiler to translate it and also follow
Pascal language standards created by Nicklaus Wirth
and K. Jensen.
• Pascal is a high-level programming language
(high level language) and structured (Structured
Programming language).

Structure of Language PASCAL
in common
1. Section Title Programs
2. Declaration Section
• Declaration of data types (TYPE)
• Declaration of variables (VAR)
• Declaration constants (CONST)
• Declaration label (LABEL)
• Declaration of sub-programs (PROCEDURE and
FUNCTION)
3. Section Main Program Statement.

Rules Prgram
• Each end of the semicolon terminated statement
(;), Except for the name of the label.
• End the program were given a period (.).

Comments
• Is the information given to
documentation purposes.
• Not result in action (not
influence the course of the program).
• Can use the sign:
(This comment) or (* this comment *)

Basic Commands / Simple Pascal
• Write ( 'Text / writing', variable);
Command to display or print
the monitor screen without moving the line
• WriteLn ( 'Text / Writing', variable);
Command to display / print screen
monitor and move the line down
• Read (variable);
Command to enter / fill data
without moving the line
• readln (variable);
Command to enter / fill data
then move the line




Basic Pascal
Programming elements
• Obtain data by reading data from
The default input (key board, files or data source
others).
• Storing data into memory with
appropriate data structures,
• Processing the data with the right instruction.
• Presenting or sending the data processed
the default output (monitor, file, or destination
others).

Identifier
• Public Identifier
Identifier is defined solely by
programmers. Programmers have the freedom
to determine the name identifiernya, with
conditions are not the same name with the identifier
standards and reserved word that will be discussed
further. This is to prevent errors
that could arise due to overlapping identifier
program

Terms of General Identifier
• Beginning with the letter
• There should be no spaces / blank
• May not use a reserved word
• May not use a special symbol,
except an underscore (sign below)
• 63 character maximum length

Examples of public Identifier
• Luas_Segi_Tiga
• LuasSegiTiga
• LuasS3
• LS3

Identifier
• Standard Identifier (Baku)
Identifier is defined by
Pascal compiler makers. Usually maker
compiler provides a library that has
There in the compiler. Library contains a variety of
procedure, function or unit that is ready to use.
Eg Windows Turbo Pascal 1.5 has a
unit to process the output of wincrt, gotoxy,
which can easily be used by programmers
in the writing program codes

Sample Standard Identifier
• abs int cos arctan dispose boolean eof
exp false eoln integer input new maxint ln
odd output Ord Pred pack page read readln
reset rewrite real sin sqr sqrt round succ
trunc true text write writeln


Identifier reserved word
• Identifier "reserved word", ie the
already defined and used by
PASCAL language itself (We can not
identifier naming us with this).



Sample Identifier Reserved word
and array begin case const div do downto
end else files for forward function goto if in
label mod nil not of or packed procedure
program record repeat set then to type
until var while with

Variable Declaration:
• Variable Declaration:
Declaring variables is:
a. Variable name as the identity
identifier
b. Determining the type of variable data
• Examples of variable declarations:
Var K: integer;
R: real;
C: char;
Q: boolean;

Similar variables
• Some similar identivier
can be declared together.
• Example var i, j, k: integer; (variable i,
j and k as an integer)
namaMHS, alamatMHS: char;
(Name and address of the student)

Constant
• Constant Declaration:
Declaring constants are:
a. Providing a constant as
user ID
b. Determining the value of the constant
• Examples of constant declarations:
const MaximumSize = 100; (integer)
Exit = 'Q'; (char)

Type Declaration
Identifier (identifier) of the data
type used should be introduced.
If you want to use the data type
name created by the user, then
should be mentioned standard data types.

Example of the Type
• Sample type
Type
Nm: String [25];
Kls: String [7];
Nil: Integer;
Gr: Char;
Var
Name: Nm;
Class: Kls;
Value: Nil;
Grade: Gr;

Sample No Type
Var Name: String [25];
Class: String [7];
Value: Integer;
Grade: Char;



Example programs:
TAMBAH_00 programs; (Sum
two numbers whose value is given
in order)
var angka1, angka2, result: integer; (
Declaration of variables as integers)
BEGIN (Main Program Start)
angka1: = 50; (command gives the value
50 in var. angka1)
angka2: = 25; (command gives the value
25 in var. angka2)
result: = angka1 + angka2; (Commands
outcome add up and save
the results)
Write (result); (command to print the results)
END. (End of Main Program)

Sample Program 2
TAMBAH_01 programs; (Menjumlahlan
two numbers are read from
default input)
bilbulat type: integer;
var angka1, angka2, results: bilbulat; (
Declaration as integer)
BEGIN (Main Program Start)
read (angka1); (Reading angka1 value
through the key-board)
read (angka2); (Reading angka2 value
through the key-board)
result: = angka1 + angka2; (Sum
and save the results to the results)
write (result); (Presenting the results to the screen
monitor)
END. (End of Main Program)