###### tags: `Pascal Programming D1`
# Search Binary(p.409)
## Ver II (6/10/2021)
program search;
type
ExamType = record
Name : string[30];
Mark : 0..100
end;
const
Num = 11;
var
Student : array[1..Num] of ExamType;
position : integer;
procedure InputData;
var
Examfile : text;
count : integer;
begin
assign(Examfile, 'exam.txt');
reset(Examfile);
count := 0;
while not eof(Examfile) do begin
count := count + 1;
with Student[count] do begin
readln(Examfile, Name);
readln(Examfile, Mark)
end;
end;
close(Examfile)
end;
procedure Search(var index : integer);
var
target : string;
found : boolean;
count : integer;
top, bottom, middle : integer;
begin
write('Enter name of student: ');
readln(target);
found := false;
count := 0;
index := 0;
top := 1;
bottom := Num;
repeat
count := count + 1;
middle := (top + bottom) div 2;
with Student[middle] do
begin
if target > Name
then top := middle + 1
else if target < Name
then bottom := middle - 1
else begin
index := middle;
found := true
end;
end;
until found or (count = Num);
writeln(' Number of iterations = ', count)
end;
procedure DisplayResult(index : integer);
begin
if index = 0
then writeln('Record not found!')
else with Student[Index] do
begin
writeln( 'Record found!');
writeln( 'Student name = ', Name);
writeln( 'Exam mark = ', Mark);
readln;
end;
end;
begin
InputData;
Search(position);
DisplayResult(position)
end.