1. Preliminary Often in programming we need a way to store multiple values at once to be used or displayed again. Such values can be a value generated in the program, or the value entered by the user. Required method for preparing a number of empty places in computer memory will only be used to store values during the program runs, and that value can be used again at any time. Pascal provides this capability by using the word backup array. An array is a structured type consisting of a number of components that have the same type. These components are called component type or base type. An array has a fixed number of components. Components can use any data type. The number of components in an array is shown by an index called the index type and must be of type ordinal. Each component of the array accessed by referencing the index value. Type an array must be a simple type, should not be of type complex. When the values of the array element value is restricted within a certain range of coverage, it can be declared with type-shaped array subrange. While hem array element values are limited to certain values that can not be written in a range, but can be determined in sequence, then you can use an array of scalar type.
2. The word used reserve Program: The specification name of the program and its parameters, is decorative and not affect the overall program itself Uses: define the name of the unit to be referenced by the program Var: linking an identifier and its type with a location in memory where the values of these types can be stored Begin: starting a program block End: end a program block Array: used to define an array type For: defines the initial value iteration To: defines the value of the final iteration (iteration positive) Do: used to describe the action to perform looping If: mention the conditions for a statement that can be executed Then: mention the statement of action must be executed when the conditions are met Else: mention the statement of actions that can be executed while another action statement is not executed because the condition is not fulfilled.
3. The procedure used Write: writing a variable into a component file in a text file used to write one or more values to a file Writeln: executing the write procedure, then pulled out a marker of end-of-line (EOL) to the program files Read: read a file into a variable component, in a text file used to read one or more values into one or more variables Readln: executes a read procedure, and then move to the next line in program files Randomize: initialize the random number generator owned by Pascal compiler with a random value obtained from the computer clock.
4. Functions used Random: giving rise to a random value Sqr: square value of an argument Sqrt: root value of an argument Trunc: cutting the real value of type integer to a value type Frac: restore the fraction of an argument
a. Contoh pertama
Program m5c1;
Uses wincrt;
Const
Huruf : array [0..4] of char = ('E','D','C','B','A');
Var
i : word;
Begin
For i := 0 to 4 do
Writeln('Nilai ke ',i, ' = ',Huruf[i]);
End.
b. Contoh ke-dua
Program m5c2;
Uses wincrt;
Var
x : array [1..100] of integer;
i, n, max, min : integer;
Begin
Write ('Banyaknya data: ');
Readln (n);
Randomize;
For i := 1 to n do
x[i] := random(100);
max := x[1];
min := x[1];
For i:= 2 to n do
Begin
If x[i] > max then max := x[i];
If x[i] < min then min := x[i];
End;
Writeln ('Maksimal: ', max, ', minimal: ', min);
End.
c. Contoh ke-tiga
Program m5c3;
Uses wincrt;
Const
max = 100;
Var
x : array [1..max] of integer;
i, n, tot : integer;
mean, sd, totkwd : real;
Begin
write ('Jumlah data? ');
readln (n);
writeln;
tot := 0;
for i := 1 to n do
Begin
write ('Masukkan data ke ',i,': ');
readln (x[i]);
tot := tot + x[i];
End;
mean := tot/n;
totkwd := 0;
for i := 1 to n do
totkwd := totkwd + sqr (mean - x[i]);
sd := sqrt (totkwd/(n-1));
writeln;
writeln ('Standar Deviasi: ',sd:5:2);
End.
d. Contoh ke-empat
Program m5c4;
Uses wincrt;
Type
X = string[7];
Const
FaktorBln: array[1..12] of byte = (0,3,3,6,1,4,6,2,5,0,3,5);
hari: array[0..8] of x = ('Minggu','Senin','Selasa',' ','Rabu','Kamis',' ','Jumat','Sabtu');
Var
i: word;
nama: string[255];
j1, j2, j3, j4: integer;
tgl, bln, thn: integer;
Begin
write ('Tanggal? '); readln(tgl);
write ('Bulan (1-12)? ');readln(bln);
write ('Tahun (19..)? ');readln(thn);
if thn > 1900 then thn := thn - 1900;
j1 := trunc (thn*365.25);
j2 := j1 + faktorbln[bln];
if (thn/4 = int(thn/4)) and (bln<3) then j2 := j2-1;
j3 := j2 + tgl;
j4 := trunc(frac(j3/7)*10);
writeln;
writeln('Harinya adalah: ',hari[j4]);
End.
Tidak ada komentar:
Posting Komentar