Recent

Author Topic: Program Stops running after you have entered the value to be found please help  (Read 1446 times)

AJM99

  • Newbie
  • Posts: 1
This is a binary search program for as-level computing. Please help.

program BinarySearch;

{$mode objfpc}{$H+}

uses SysUtils;
type num_list=array[1..10]of integer;
var
  Found: boolean;
  Middle,Low,High,x,i: integer;
  List: num_list;
begin
    i:=1;
    Repeat
      begin
        Writeln('Enter a number');
        Readln(List);
        i:=i+1;
      end;
    until i=11;
    Writeln('Enter value to be found');
    Readln(x);
    begin
      Low:=List[1];
      High:=List[10];
      Found:=False;
      Repeat
        Begin
          Middle:=Trunc((Low+High)/2);
          IF List[Middle]=x THEN
            Found:=True
          ELSE
            IF List[Middle]>x THEN
              High:=Middle-1
            ELSE
              Low:=Middle+1;
        end;
      UNTIL Found=True;
    end;
    IF Found=True THEN
        Writeln('The number was found')
    ELSE
      Writeln('The number was not found');
    Readln;
end.

balazsszekely

  • Guest
The program is running, but you have a runaway loop.
repeat
//...
until Found = True; //this condition is never met, I let you figure out why

 

TinyPortal © 2005-2018