Pada pemantapan PASCAL saya akan mengshare ke anda contoh-contoh pemrograman pascal yang bisa untuk kalian explore lebih dalam.
Contoh Program PASCAL mencari bilangan biner :
PROGRAM binary_search;
{Program to search a number using binary search}
USES crt;
TYPE index=1..100;
VAR
arr:ARRAY[1..100] OF index;
VAR mid,low,high,search:integer;
i,n:index;
found:boolean;
BEGIN
clrscr;
writeln('BINARY SEARCH');
writeln('Enter the array size');
readln(n);
writeln('Enter the array elements');
FOR i:=1 TO n DO
BEGIN
readln(arr[i]);
END;
writeln('Enter the search element');
readln(search);
low:=1;
high:=n;
found:=false;
REPEAT
mid:=trunc(low+high) DIV 2;
IF (search<arr[mid]) THEN
high:=mid-1;
IF (search>arr[mid]) THEN
low:=mid+1;
IF (search=arr[mid]) THEN
found:=true
ELSE
found:=false;
UNTIL ((found=true) OR (high<low));
IF found=true THEN writeln('ELEMENT FOUND')
ELSE writeln('ELEMENT NOT FOUND');
END.
Contoh program pascal mencari bilangan linear :
PROGRAM linear_search; {Program to search a number using linear search}
USES crt;
TYPE index=1..100;
VAR n,searchkey,i:integer;
found:boolean;
arr:ARRAY[1..100] OF index;
BEGIN
writeln('LINEAR SEARCH');
writeln('Enter the boundary of the array');
readln(n);
writeln('Enter the array elements');
FOR i:=1 TO n DO
BEGIN
readln(arr[i]);
END;
i:=1;
found:=false;
writeln('Enter the search element');
readln(searchkey);
WHILE ((i<=n) AND (found=false)) DO
BEGIN
IF arr[i]=searchkey THEN
found:=true
ELSE found:=false;
i:=i+1;
END;
IF found=true THEN
writeln('ELEMENT FOUND')
ELSE
writeln('ELEMENT NOT FOUND');
END.
Contoh program pascal membuat gelembung angka 0 dengan array :
PROGRAM bubble_sort;
uses crt;
CONST items=100;
VAR n,temp,pass,index:integer;
sorted:boolean;
vector:ARRAY[1..items] of integer;
PROCEDURE sort;
BEGIN
pass:=1;
REPEAT
sorted:=true;
FOR index:=1 TO items-pass DO
BEGIN
IF vector[index]>vector[index+1] THEN
BEGIN
sorted:=false;
temp:=vector[index];
vector[index]:=vector[index+1];
vector[index+1]:=temp;
END;
END;
pass:=pass+1;
UNTIL sorted;
END;
BEGIN
writeln('How many elements');
readln(n);
writeln('Enter the unsorted elements');
index:=1;
REPEAT
readln(vector[index]);
index:=index+1;
UNTIL index=n+1;
sort;
writeln('Sorted Data');
FOR index:=1 TO items DO
BEGIN
IF ((index-1) MOD 10)=0 THEN writeln;
writeln(vector[index]:4);
END;
writeln('Total number of passes=> ',pass);
writeln;
END.
Contoh program pascal membuat urutan nos angka sesuai inputan array:
PROGRAM insertion_sort; {Program to sort the given nos using insertion sort}
USES crt;
VAR a:ARRAY[1..100] of real;
VAR temp:real;
i,j,n:integer;
BEGIN
clrscr;
writeln('Enter the boundary of the array');
readln(n);
writeln('Enter the elements of the array');
FOR i:=1 TO n DO
BEGIN
readln(a[i]);
END;
FOR i:=2 TO n DO
BEGIN
j:=i-1;
WHILE ((j>=1) AND (a[j+1]<a[j])) DO
BEGIN
temp:=a[j];
a[j]:=a[j+1];
a[j+1]:=temp;
j:=j-1;
END;
END;
writeln('The sorted elements are as follows');
FOR i:=1 TO n DO
writeln(a[i]);
END.
Contoh program pascal membuat urutan angka seperti fibonanci acak:
Program QSort; {$R-,S-}
uses Crt;
{ This program demonstrates the quicksort algorithm, which }
{ provides an extremely efficient method of sorting arrays in }
{ memory. The program generates a list of 1000 random numbers }
{ between 0 and 29999, and then sorts them using the QUICKSORT }
{ procedure. Finally, the sorted list is output on the screen. }
{ Note that stack and range checks are turned off (through the }
{ compiler directive above) to optimize execution speed. }
const
Max = 1000;
type
List = array[1..Max] of Integer;
var
Data: List;
I: Integer;
{ QUICKSORT sorts elements in the array A with indices between }
{ LO and HI (both inclusive). Note that the QUICKSORT proce- }
{ dure provides only an "interface" to the program. The actual }
{ processing takes place in the SORT procedure, which executes }
{ itself recursively. }
procedure QuickSort(var A: List; Lo, Hi: Integer);
procedure Sort(l, r: Integer);
var
i, j, x, y: integer;
begin
i := l; j := r;
x := a[(l+r) DIV 2];
repeat
while a[i] < x do i := i + 1;
while x < a[j] do j := j - 1;
if i <= j then
begin
y := a[i];
a[i] := a[j];
a[j] := y;
i := i + 1;
j := j - 1;
end;
until i > j;
if l < j then Sort(l, j);
if i < r then Sort(i, r);
end;
begin {QuickSort};
Sort(Lo,Hi);
end;
begin {QSort}
Write('Now generating 1000 random numbers...');
Randomize;
for i := 1 to Max do Data[i] := Random(30000);
Writeln;
Write('Now sorting random numbers...');
QuickSort(Data, 1, Max);
Writeln;
for i := 1 to 1000 do Write(Data[i]:8);
end.
Contoh program pascal membuat fibonanci series :
PROGRAM fibonacci_series;
VAR a,b,j,n:integer;
PROCEDURE fib(a,b,j:integer);
BEGIN
IF j>0 THEN
BEGIN
WHILE j<>a DO
BEGIN
writeln(a:1,' ');
fib(b,a+b,j-1);
END;
END;
END;
BEGIN
writeln('FIBONACCI SERIES');
writeln;
writeln('Enter any number');
readln(n);
writeln;
IF n<=0 THEN writeln('Invalid Entry,please try again!')
ELSE
fib(0,1,n);
END.
MATERI PASCAL
Jika kalian baru dalam pembelajaran pemrograman Pascal, Silakan belajar dari materi pertama :
Materi Ke-1 : Perintah Output
Materi Ke-2 : Perintah Input
Materi Ke-3 : Looping / Perulangan
Materi Ke-4 : Nested Loop
Materi Ke-5 : Percabangan (IF ELSE)
Materi Ke-6 : Perintah Loncat (GoTo)
Materi Ke-7 : Array
Materi Ke-8 : Array Lanjutan
Materi Ke-9 : Procedure
Materi Ke-10 : Function
Materi Ke-11 : Rekursif
Materi Ke-12 : Pemantapan Contoh Contoh Program Pascal
Materi Ke-2 : Perintah Input
Materi Ke-3 : Looping / Perulangan
Materi Ke-4 : Nested Loop
Materi Ke-5 : Percabangan (IF ELSE)
Materi Ke-6 : Perintah Loncat (GoTo)
Materi Ke-7 : Array
Materi Ke-8 : Array Lanjutan
Materi Ke-9 : Procedure
Materi Ke-10 : Function
Materi Ke-11 : Rekursif
Materi Ke-12 : Pemantapan Contoh Contoh Program Pascal
Comments