Help with binary search - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with binary search

uneducated_tweuneducated_twe Posts: 8Member
[b][red]This message was edited by uneducated_twe at 2004-3-10 4:26:31[/red][/b][hr]
[b][red]This message was edited by uneducated_twe at 2004-3-10 4:25:1[/red][/b][hr]
hi, i've got another problem with a binary search. here's the code:

[code]procedure search_rental_rental_ID;
begin
with rental_record do
begin
repeat
clrscr;
reset(rental_file);
writeln('Please enter the ID number of the record you wish to view');
readln(searched_rental_ID);

record_exists := false;
search_failed := false;
top := counter - 1;
bottom := 0;

repeat
midpoint := Trunc((top + bottom) / 2);
seek(rental_file,midpoint);
read(rental_file,rental_record);

if rental_record.rental_ID = searched_rental_ID then
begin
record_exists := true;
clrscr;
writeln('ID : ',rental_ID);
writeln('Customer ID : ',customer_ID);
writeln('Title ID : ',title_ID);
writeln('Date Issued : ',date_issued,'/',month_issued,'/',year_issued);
writeln('Date Due Back : ',date_due,'/',month_due,'/',year_due);
readln;

if rental_record.rental_ID <> searched_rental_ID then
begin
if bottom > top then search_failed := true;
if bottom <= top then
begin
if rental_record.rental_ID < searched_rental_ID then bottom := midpoint + 1;
if rental_record.rental_ID >= searched_rental_ID then top := midpoint - 1;
end;
end;
end;

until (record_exists = true) or (search_failed = true);

if search_failed = true then writeln('No matching records found');

close(rental_file);

repeat
writeln('Do you wish to search for another record? (y/n)');
readln(answer_y_n);
until answer_y_n in ['Y','y','N','n'];

until answer_y_n in ['N','n'];
end;
end;[/code]

when i try to compile, it claims the very last line in the program (END.) should be (END;). ie changing the full stop to a semi-colon. i don't understand?? if i take out one end from the 3 at the end of the if statements, it says there is an error in statement.

all variables are there, except counter which is the number of records in the file.


Comments

  • adibicheaadibichea Posts: 46Member
    procedure look fine
    verify other procedure if "end missing"

  • uneducated_tweuneducated_twe Posts: 8Member
    it's definitely that procedure. when i comment it out, the program compiles fine.
  • zibadianzibadian Posts: 6,349Member
    [b][red]This message was edited by zibadian at 2004-3-12 14:2:44[/red][/b][hr]
    : it's definitely that procedure. when i comment it out, the program compiles fine.
    :
    I've copied the procedure from your post and compiled it successfully, so the procedure is balanced in begin's and end's. I suggest, that you make sure that the indentation is correct: each end should be indented at the same level as its matching begin. And everything between needs to be indented 1 level higher. The same goes for repeat-until and case-end combos. Here's your code with the correct indentation as postulated by the Borland company:
    [code]
    procedure search_rental_rental_ID;
    begin
    with rental_record do
    begin
    repeat
    clrscr;
    reset(rental_file);
    writeln('Please enter the ID number of the record you wish to view');
    readln(searched_rental_ID);
    record_exists := false;
    search_failed := false;
    top:= counter - 1;
    bottom:= 0;
    repeat
    midpoint:= Trunc((top + bottom) / 2);
    seek(rental_file,midpoint);
    read(rental_file,rental_record);
    if rental_record.rental_ID = searched_rental_ID then
    begin
    record_exists := true;
    clrscr;
    writeln('ID: ',rental_ID);
    writeln('Customer ID: ',customer_ID);
    writeln('Title ID: ',title_ID);
    writeln('Date Issued: ',date_issued,'/',month_issued,'/',year_issued);
    writeln('Date Due Back : ',date_due,'/',month_due,'/',year_due);
    readln;
    if rental_record.rental_ID <> searched_rental_ID then
    begin
    if bottom > top then
    search_failed := true;
    if bottom <= top then
    begin
    if rental_record.rental_ID < searched_rental_ID then
    bottom := midpoint + 1;
    if rental_record.rental_ID >= searched_rental_ID then
    top := midpoint - 1;
    end;
    end;
    end;
    until (record_exists = true) or (search_failed = true);
    if search_failed = true then
    writeln('No matching records found');
    close(rental_file);
    repeat
    writeln('Do you wish to search for another record? (y/n)');
    readln(answer_y_n);
    until answer_y_n in ['Y','y','N','n'];
    until answer_y_n in ['N','n'];
    end;
    end;
    [/code]


Sign In or Register to comment.