Sequential Search
Salam, berikut ini diberikan contoh program menggunakan Sequential Search menggunakan bahasa pemrograman Pascal. Silhakan saudara coba coding menggunakan tools Free pascal.
program pencarian_SequenctialSearch;
uses crt;
const
Nmaks = 100;
type
larikInt = array[1..Nmaks] of integer;
var
L : larikint;
i : integer; {indeks Larik}
n : integer; {banyaknya elemen Larik used}
x : integer; {nilai dicaari}
idx : integer;
procedure bacaLarik(var L:larikInt; n : integer);
var
i : integer;
begin
for i := 1 to n do
begin
write('Masukan nilai L[',i,']:'); readln(L[i]);
end;
end;
procedure cetakLarik(L:larikInt; n : integer);
var
i : integer;
begin
for i := 1 to n do
begin
writeln(L[i]);
end;
end;
procedure seqSearch2(L : LarikInt; n : integer; x : integer; var idx:integer);
var
i : integer;
begin
i := 1;
while (i < n) and (L[i] <> x) do
begin
i := i + 1;
end;
if L[i] = x then
idx := i
else
idx := -1;
end;
begin
clrscr;
write('Berapa jumlah data (n) : '); readln(n);
writeln('Baca data: ');
BacaLarik(L,n);
write('Ketikan nilai dicari: '); readln(x);
seqSearch2(L, n, x, idx);
if idx = -1 then
writeln('Tidak ditemukan!')
else
writeln(x, ' ditemukan pada index ke-', idx);
repeat until keypressed;
readln;
end.
program pencarian_SequenctialSearch;
uses crt;
const
Nmaks = 100;
type
larikInt = array[1..Nmaks] of integer;
var
L : larikint;
i : integer; {indeks Larik}
n : integer; {banyaknya elemen Larik used}
x : integer; {nilai dicaari}
idx : integer;
procedure bacaLarik(var L:larikInt; n : integer);
var
i : integer;
begin
for i := 1 to n do
begin
write('Masukan nilai L[',i,']:'); readln(L[i]);
end;
end;
procedure cetakLarik(L:larikInt; n : integer);
var
i : integer;
begin
for i := 1 to n do
begin
writeln(L[i]);
end;
end;
procedure seqSearch2(L : LarikInt; n : integer; x : integer; var idx:integer);
var
i : integer;
begin
i := 1;
while (i < n) and (L[i] <> x) do
begin
i := i + 1;
end;
if L[i] = x then
idx := i
else
idx := -1;
end;
begin
clrscr;
write('Berapa jumlah data (n) : '); readln(n);
writeln('Baca data: ');
BacaLarik(L,n);
write('Ketikan nilai dicari: '); readln(x);
seqSearch2(L, n, x, idx);
if idx = -1 then
writeln('Tidak ditemukan!')
else
writeln(x, ' ditemukan pada index ke-', idx);
repeat until keypressed;
readln;
end.