Data Structure Practice 3rd Meeting

In this 3rd meeting, my class talk about searching . Weather searching by using sorted data or unsorted data.

here is my programs :

1.  Sequensial search By using unsorted data


uses wincrt;
var
data:array[1..10]of char;
a,b,flag:byte;
c:integer;
cari,z,s:string;
temu:boolean;
d:char;
begin
flag:=0;
temu:=true;
write('data  =');read(z);
b:=length(z);

 for a:= 1 to b do
 begin
 data[a]:=z[a];
 end;
 write('cari = ');readln;read(cari);

 d:=cari[1];
a:=0;
while  a<=b do
 begin
 a:=a+1;
 if d=data[a] then
 begin
 flag:=1;
 temu:=true;
 z:='ditemukan';
 c:=a-1;
 a:=b;
 end
 else
 temu:=false;

end;

if (temu<>true) and (flag<>1) then
begin
c:=-1;
writeln('DATA YANG ANDA CARI = tidak ada,index = ',c);
exit;
end
else
begin
writeln('DATA YANG ANDA CARI = ada ','index = ',c);
exit;
end;
end.

2.  Binary search By using sorted data


uses wincrt;
var
data:array[1..10]of char;
flag,a,hi,lo,mid:byte;
cari,z:string;
temu:boolean;
d:char;
c:integer;
begin
lo:=1;
temu:=false;
flag:=0;
write('data  =');read(z);
hi:=length(z);
 for a:= 1 to hi do
 begin
 data[a]:=z[a];
 end;
 write('cari = ');readln;read(cari);

d:=cari[1];

while  (lo<=hi) and (not temu) do
 begin
 mid:=(lo+hi) div 2;
 if data[mid]=d then
 begin
 flag:=1;
 temu:=true;
 c:=mid-1;
 lo:=hi;
 end
 else
 if data[mid]<d then
 lo:=mid+1
 else
 hi:=mid-1
end;

if (temu<>true) and (flag<>1) then
begin
c:=-1;
writeln('data tidak ditemukan = (',z,' ,',cari,')');
writeln('index = ', c);
end
else
begin
writeln('data ditemukan = (',z,' ,',cari,')');
write('index = ',c);
end;
end.

3.   Find out a word within some data and show them all


program budi;
uses wincrt;
var
data:array [1..10,1..100]of char;
panjang:array[1..10]of byte;
lo,flag,a,b,c:byte;
kata:string;
cari:string;
car:char;
temu:boolean;
temp:array[1..10,1..100]of char;
tamp:array[1..10]of string;
begin
flag:=0;
temu:=false;
write('masukkan data total = ');read(b);
 for a:=1 to b do
 begin
 write('masukkan panjang kata ke-',a,'=');read(c);
 panjang[a]:=c;
 writeln('INDEX[',a,']');
 for lo:=1 to panjang[a] do
 begin
 write('huruf[',lo,']=');readln;read(data[a,lo]);
 end;
 end;

write('masukkan data yang akan dicari = ');readln;read(cari);
car:=cari[1];

for a:=1 to b do
begin
 for lo:=1 to panjang[a] do
 begin
 if car=data[a,lo] then
 begin
 temu:=true;
 flag:=1;
 temp[a,lo]:=data[a,lo];
 tamp[a]:=data[a]
 end
 else
 end;
 end;

 if (temu=true) and (flag=1) then
 begin
 writeln('ketemu');
 for a:=1 to b do
 begin
 write(tamp[a]);
 writeln;
 end;
 end
 else
 writeln('no temu');
end.

4.  Sequensial search By using unsorted data to find out the highest average


program mahasiswa;
uses wincrt;
type data=record
nrp:string;
nama:string;
matkul:string;
ntugas:real;
nquiz:real;
nuts:real;
nuas:real;
rata:real;
end;
var
mhs:array[1..10] of data;
a,b:byte;
tinggi:real;
lagi:char;
begin
repeat
clrscr;
write('masukkan jumlah mahasiswa = ');read(a);
 for b:=1 to a do
 begin
 writeln;
 writeln('DATA MAHASISWA KE-',b);
 write('masukkan nrp mahasiswa         = ');readln;read(mhs[b].nrp);
 write('masukkan nama mahasiswa        = ');readln;read(mhs[b].nama);
 write('masukkan mata kuliah mahasiswa = ');readln;read(mhs[b].matkul);
 write('masukkan nilai tugas mahasiswa = ');readln;read(mhs[b].ntugas);
 write('masukkan nilai quiz mahasiswa  = ');readln;read(mhs[b].nquiz);
 write('masukkan nilai UTS mahasiswa   = ');readln;read(mhs[b].nuts);
 write('masukkan nilai UAS mahasiswa   = ');readln;read(mhs[b].nuas);
 end;

 for b:=1 to a do
 begin
 mhs[b].rata:=(10/100*mhs[b].ntugas)+(10/100*mhs[b].nquiz)+(30/100*mhs[b].nuts)+(50/100*mhs[b].nuas)/a;
 end;

 clrscr;
 writeln('================================================================================');
 gotoxy(3,2);write('NRP');
 gotoxy(12,2);write('NAMA');
 gotoxy(29,2);write('MATA KULIAH');
 gotoxy(49,2);write('TUGAS');
 gotoxy(57,2);write('QUIZ');
 gotoxy(64,2);write('UTS');
 gotoxy(69,2);write('UAS');
 gotoxy(75,2);write('RATA');
 writeln;
 writeln('================================================================================');
 for b:=1 to a do
 begin
 gotoxy(1,b+3);write(mhs[b].nrp);
 gotoxy(12,b+3);write(mhs[b].NAMA);
 gotoxy(32,b+3);write(mhs[b].MATKUL);
 gotoxy(50,b+3);write(mhs[b].NTUGAS:0:0);
 gotoxy(58,b+3);write(mhs[b].NQUIZ:0:0);
 gotoxy(64,b+3);write(mhs[b].NUTS:0:0);
 gotoxy(70,b+3);write(mhs[b].NUAS:0:0);
 gotoxy(75,b+3);write(mhs[b].RATA:0:0);
 writeln;
 end;
 writeln('================================================================================');

 writeln('tekan enter untuk melihat hasil pencarian nilai rata- rata tertinggi');
 readln;
 readln;
 {proses pencarian data rata - rata yang tertinggi}
 clrscr;
 tinggi:=mhs[1].rata;
 writeln('MAHASISWA YANG MEMILIKI NILAI RATA - RATA TERTINGGI ADALAH');
 for b:=1 to a do
 begin
 if tinggi>mhs[b].rata then
 begin
 writeln('nrp mahasiswa            = ',mhs[1].nrp);
 writeln('nama mahasiswa           = ',mhs[1].nama);
 writeln('mata kuliah mahasiswa    = ',mhs[1].matkul);
 writeln('nilai tugas mahasiswa    = ',mhs[1].ntugas:0:0);
 writeln('nilai quiz mahasiswa     = ',mhs[1].nquiz:0:0);
 writeln('nilai UTS mahasiswa      = ',mhs[1].nuts:0:0);
 writeln('nilai UAS mahasiswa      = ',mhs[1].nuas:0:0);
 writeln('nilai RATA 2 mahasiswa   = ',mhs[1].rata:2:2);
 end
 else if tinggi<mhs[b].rata then
 begin
 tinggi:=mhs[b].rata;
 writeln('nrp mahasiswa            = ',mhs[b].nrp);
 writeln('nama mahasiswa           = ',mhs[b].nama);
 writeln('mata kuliah mahasiswa    = ',mhs[b].matkul);
 writeln('nilai tugas mahasiswa    = ',mhs[b].ntugas:0:0);
 writeln('nilai quiz mahasiswa     = ',mhs[b].nquiz:0:0);
 writeln('nilai UTS mahasiswa      = ',mhs[b].nuts:0:0);
 writeln('nilai UAS mahasiswa      = ',mhs[b].nuas:0:0);
 writeln('nilai RATA 2 mahasiswa   = ',mhs[b].rata:2:2);
 end;
 end;
write('lagi =');read(lagi);
until (lagi='t') or (lagi='T');
end.

5.  Sentinel search


program sentinel_searching;
uses wincrt;
var
data:array[1..10]of integer;
c,e:integer;
a,b,d:byte;
ketemu:boolean;
begin
ketemu:=false;
write('masukkan jumlah data total = ');read(a);

 for b:=1 to a-1 do
 begin
 write('INDEX [',b,']=');read(data[b]);
 end;

write('masukkan data yang akan dicari = ');read(c);
data[a]:=c;
b:=0;

 while (data[b]<>c) do
 begin
 if (b<a) then
 begin
 b:=b+1;
 e:=b;
 end
 else
 begin
 e:=b;
 end;
 end;


 if (e<>0) and (e<>a)  then
 writeln('data ditemukan di index = ',e)
 else if (e=a) or (e=0) then
 begin
 e:=-1;
 writeln('data tidak ditemukan. INDEX = ',e);
 end;
end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: