Address Book Search Program

I have been given a task to do for a college assignment. It is to find or create a program which allows entries of an address book to be displayed depending on a unique identifier entered (e.g. "PersonNumber") and write about how the program works.

The entries of the address book should be stored on a text file. The entries have to include a PersonNumber or ID, First Name, Surname, Address, Phone Number. The program has to use a binary search algorithm. The program does not need to add any entries to the text file or remove entries -- the program only needs to read from the text file using binary searches.

My problem is that I am rubbish when it comes to programming and I could not find such a program that does this in psacal. Will someone please build me a basic program (just reading from the text file - never mind about adding or removing data)
«1

Comments

  • : I have been given a task to do for a college assignment. It is to
    : find or create a program which allows entries of an address book to
    : be displayed depending on a unique identifier entered (e.g.
    : "PersonNumber") and write about how the program works.
    :
    : The entries of the address book should be stored on a text file. The
    : entries have to include a PersonNumber or ID, First Name, Surname,
    : Address, Phone Number. The program has to use a binary search
    : algorithm. The program does not need to add any entries to the text
    : file or remove entries -- the program only needs to read from the
    : text file using binary searches.
    :
    : My problem is that I am rubbish when it comes to programming and I
    : could not find such a program that does this in psacal. Will someone
    : please build me a basic program (just reading from the text file -
    : never mind about adding or removing data)
    :
    A pretty tall order since it's next to impossible to do a binary search of a text file.


  • : A pretty tall order since it's next to impossible to do a binary
    : search of a text file.

    How about if all the data is read from the text file and put into elements of an array.
  • : : A pretty tall order since it's next to impossible to do a binary
    : : search of a text file.
    :
    : How about if all the data is read from the text file and put into
    : elements of an array.
    :
    That would work. The problem is that the array would have to fit in memory. In Turbo Pascal the maximum number of records that you could fit in memory, given the data you specified, would be about 1000. And that's not leaving any room in memory for the program. If you settle for only 500 records there should be more than enough room for the program.

  • : : : A pretty tall order since it's next to impossible to do a binary
    : : : search of a text file.
    : :
    : : How about if all the data is read from the text file and put into
    : : elements of an array.
    : :
    : That would work. The problem is that the array would have to fit in
    : memory. In Turbo Pascal the maximum number of records that you
    : could fit in memory, given the data you specified, would be about
    : 1000. And that's not leaving any room in memory for the program.
    : If you settle for only 500 records there should be more than enough
    : room for the program.
    :
    :
    The story is a bit different. The maximum size of any variable is only 65535 bytes. Since a untruncated string is 256 bytes long, you can at most make a string array of 255 strings. If you truncate the string elements in your array, you can create more elements. The way to calculate the maximum number of elements is: 65535 / (length of string+1). Thus with strings of 20 characters, you are limited to 3120 (65535/(20+1)) elements.
    This calculation also holds true for other variable types.
    TP programs themselves have an upper limit of 640000 bytes of memory, minus any other in low memory running programs.
    If you need more memory than that, then TP7 (and perhaps TP6) include a class to use the memory above the first MB as storage: TEMSStream. This can be used as an array of byte with a maximum size of the EMS memory. With some maths added to it, it can easily be converted into an array of any type, as long as the records have a fixed length. This allows for the array to become larger than the 64 kB limit, although the performance is slightly lower and the records are still limited to 64 kB.
  • : : : : A pretty tall order since it's next to impossible to do a binary
    : : : : search of a text file.
    : : :
    : : : How about if all the data is read from the text file and put into
    : : : elements of an array.
    : : :
    : : That would work. The problem is that the array would have to fit in
    : : memory. In Turbo Pascal the maximum number of records that you
    : : could fit in memory, given the data you specified, would be about
    : : 1000. And that's not leaving any room in memory for the program.
    : : If you settle for only 500 records there should be more than enough
    : : room for the program.
    : :
    : :
    : The story is a bit different. The maximum size of any variable is
    : only 65535 bytes. Since a untruncated string is 256 bytes long, you
    : can at most make a string array of 255 strings. If you truncate the
    : string elements in your array, you can create more elements. The way
    : to calculate the maximum number of elements is: 65535 / (length of
    : string+1). Thus with strings of 20 characters, you are limited to
    : 3120 (65535/(20+1)) elements.
    : This calculation also holds true for other variable types.
    : TP programs themselves have an upper limit of 640000 bytes of
    : memory, minus any other in low memory running programs.
    : If you need more memory than that, then TP7 (and perhaps TP6)
    : include a class to use the memory above the first MB as storage:
    : TEMSStream. This can be used as an array of byte with a maximum size
    : of the EMS memory. With some maths added to it, it can easily be
    : converted into an array of any type, as long as the records have a
    : fixed length. This allows for the array to become larger than the 64
    : kB limit, although the performance is slightly lower and the records
    : are still limited to 64 kB.

    I was assuming that the information he specified could be held in a string[63], which would give 1000 records in a 64K array. I like to keep it simple meaning keep it all on the stack if you can. The K.I.S.S. principle.

  • : : : : : A pretty tall order since it's next to impossible to do a binary
    : : : : : search of a text file.
    : : : :
    : : : : How about if all the data is read from the text file and put into
    : : : : elements of an array.
    : : : :
    : : : That would work. The problem is that the array would have to fit in
    : : : memory. In Turbo Pascal the maximum number of records that you
    : : : could fit in memory, given the data you specified, would be about
    : : : 1000. And that's not leaving any room in memory for the program.
    : : : If you settle for only 500 records there should be more than enough
    : : : room for the program.
    : : :
    : : :
    : : The story is a bit different. The maximum size of any variable is
    : : only 65535 bytes. Since a untruncated string is 256 bytes long, you
    : : can at most make a string array of 255 strings. If you truncate the
    : : string elements in your array, you can create more elements. The way
    : : to calculate the maximum number of elements is: 65535 / (length of
    : : string+1). Thus with strings of 20 characters, you are limited to
    : : 3120 (65535/(20+1)) elements.
    : : This calculation also holds true for other variable types.
    : : TP programs themselves have an upper limit of 640000 bytes of
    : : memory, minus any other in low memory running programs.
    : : If you need more memory than that, then TP7 (and perhaps TP6)
    : : include a class to use the memory above the first MB as storage:
    : : TEMSStream. This can be used as an array of byte with a maximum size
    : : of the EMS memory. With some maths added to it, it can easily be
    : : converted into an array of any type, as long as the records have a
    : : fixed length. This allows for the array to become larger than the 64
    : : kB limit, although the performance is slightly lower and the records
    : : are still limited to 64 kB.
    :
    : I was assuming that the information he specified could be held in a
    : string[63], which would give 1000 records in a 64K array. I like to
    : keep it simple meaning keep it all on the stack if you can. The
    : K.I.S.S. principle.
    :
    :
    With a 1000 element array of string[63], there is still enough room for the program. Especially when the array is declared as a global variable. That way it is stored on the heap, which is much larger than the stack.
  • I don't know if this helps, but I have not populated a text file yet and I know there are not going to be many records (20 Max). I just need a working program that uses a binary search so I can explain how it works. Any help would be greatly appreciated.
  • : I don't know if this helps, but I have not populated a text file yet
    : and I know there are not going to be many records (20 Max). I just
    : need a working program that uses a binary search so I can explain
    : how it works. Any help would be greatly appreciated.
    :
    Here is a binary search through an array of string. This code assumes a 0-indexed array (i.e. array[0..SomeLength] of string).
    [code]
    Low := 0;
    High := Length(StringArray)-1;
    Result := -1;
    if StringArray[Low] = SearchValue then
    Result := Low
    else if StringArray[High] = SearchValue then
    Result := High
    else while (Low <> High) and (Low+1 <> High) do
    begin
    Mid := (High-Low) div 2 + Low;
    if StringArray[Mid] = SearchValue then
    begin
    Result := Mid;
    Break;
    end else if StringArray[Mid] > SearchValue then
    High := Mid
    else
    Low := Mid;
    end;
    if StringArray[Low] = SearchValue then
    Result := Low
    else if StringArray[High] = SearchValue then
    Result := High;
    if Result = -1 then
    writeln(SearchValue+' not found!')
    else
    writeln(SearchValue+' has an index of '+Result);
    [/code]
  • Thank you very much. but can you provide a full program. I'm finding this very difficult. Also, how would I populate the text file. Would it be something like this:

    1 Firstname Surname Address PhoneNo
    2 Another Name Address PhoneNo

    I am using Turbo Pascal 1.5 for Windows
  • : Thank you very much. but can you provide a full program. I'm finding
    : this very difficult. Also, how would I populate the text file. Would
    : it be something like this:
    :
    : 1 Firstname Surname Address PhoneNo
    : 2 Another Name Address PhoneNo
    :
    : I am using Turbo Pascal 1.5 for Windows
    :
    I don't know. It depends on how much information is in the address, how the formatting of the data is, etc. Theoretically you can fill the text file with anything, as long as the read part of the program can handle the format.
    Here's a simple reader, which fills the string array with the lines form the text file:
    [code]
    Assign(f, 'filename.txt');
    reset(f);
    i := 0;
    while not eof(f) do
    begin
    readln(f, StringArray[i]);
    inc(i);
    end;
    Close(f);
    ArrayLength := i;
    [/code]
  • This task is proving I know very little about programming. What am I doing wrong?
    I'm using Turbo Pascal 1.5 for Windows and I don't think the compilor I am using supports the Break command. When I remove the Break, the program runs but it doesn't do what it is supposed to. If I enter "1" as the Person ID, "1 not found!" is displayed and when I enter "2" or any number above 1, the program seems like it freezes (it takes up 99% CPU Usage).

    At the moment, my text file is populated with the following data:

    [code]1 John Smith 123 Fake Street (11111) 111111
    2 Pat Evans 31 Albert Square (22222) 222222[/code]


    The code:
    [code]Program Search;

    Uses
    WinCrt;

    Var
    f: Text;
    i: Integer;
    StringArray: Array[0..20] of String;
    ArrayLength: Integer;
    Low, Mid, High: Integer;
    Result: Integer;
    SearchValue: String;

    Begin
    assign(f, 'filename.txt');
    reset(f);
    i := 0;
    while not eof(f) do
    begin
    readln(f, StringArray[i]);

    write('Enter Person ID to see contact details: ');
    readln(SearchValue);

    inc(i);
    Low := 0;
    High := Length(StringArray[i])-1;
    Result := -1;
    if StringArray[Low] = SearchValue then
    Result := Low
    else if StringArray[High] = SearchValue then
    Result := High
    else while (Low <> High) and (Low+1 <> High) do
    begin
    Mid := (High-Low) div 2 + Low;
    if StringArray[Mid] = SearchValue then
    begin
    Result := Mid;
    Break;
    end else if StringArray[Mid] > SearchValue then
    High := Mid
    else
    Low := Mid;
    end;
    if StringArray[Low] = SearchValue then
    Result := Low
    else if StringArray[High] = SearchValue then
    Result := High;
    if Result = -1 then
    writeln(SearchValue+' not found!')
    else
    writeln(SearchValue+' has an index of ',Result);
    end;
    Close(f);
    ArrayLength := i;
    End.[/code]
  • : This task is proving I know very little about programming. What am I
    : doing wrong?
    : I'm using Turbo Pascal 1.5 for Windows and I don't think the
    : compilor I am using supports the Break command. When I remove the
    : Break, the program runs but it doesn't do what it is supposed to. If
    : I enter "1" as the Person ID, "1 not found!" is displayed and when I
    : enter "2" or any number above 1, the program seems like it freezes
    : (it takes up 99% CPU Usage).
    :
    : At the moment, my text file is populated with the following data:
    :
    : [code]: 1 John Smith 123 Fake Street (11111) 111111
    : 2 Pat Evans 31 Albert Square (22222) 222222[/code]:
    :
    :
    : The code:
    : [code]: Program Search;
    :
    : Uses
    : WinCrt;
    :
    : Var
    : f: Text;
    : i: Integer;
    : StringArray: Array[0..20] of String;
    : ArrayLength: Integer;
    : Low, Mid, High: Integer;
    : Result: Integer;
    : SearchValue: String;
    :
    : Begin
    : assign(f, 'filename.txt');
    : reset(f);
    : i := 0;
    : while not eof(f) do
    : begin
    : readln(f, StringArray[i]);
    :
    : write('Enter Person ID to see contact details: ');
    : readln(SearchValue);
    :
    : inc(i);
    : Low := 0;
    : High := Length(StringArray[i])-1;
    : Result := -1;
    : if StringArray[Low] = SearchValue then
    : Result := Low
    : else if StringArray[High] = SearchValue then
    : Result := High
    : else while (Low <> High) and (Low+1 <> High) do
    : begin
    : Mid := (High-Low) div 2 + Low;
    : if StringArray[Mid] = SearchValue then
    : begin
    : Result := Mid;
    : Break;
    : end else if StringArray[Mid] > SearchValue then
    : High := Mid
    : else
    : Low := Mid;
    : end;
    : if StringArray[Low] = SearchValue then
    : Result := Low
    : else if StringArray[High] = SearchValue then
    : Result := High;
    : if Result = -1 then
    : writeln(SearchValue+' not found!')
    : else
    : writeln(SearchValue+' has an index of ',Result);
    : end;
    : Close(f);
    : ArrayLength := i;
    : End.[/code]:
    :
    The Break is necessary to speed up the algorithm. See http://www.programmersheaven.com/mb/pasprog/370952/371008/ReadMessage.aspx?S=B20000#371008 for an alternative.
  • : The Break is necessary to speed up the algorithm. See
    : http://www.programmersheaven.com/mb/pasprog/370952/371008/ReadMessage
    : .aspx?S=B20000#371008 for an alternative.

    Using GoTo hasn't made any difference - the program behaves in the same way if the Break command was not used. And the program does not work. I can type any number equal to or below 1 and "n not found!" is displayed. If I type a number greater than 1 the program consumes a lot of CPU usage.

    Here's teh code:

    [code]Program Search;

    Uses
    WinCrt;

    Var
    f: Text;
    i: Integer;
    StringArray: Array[0..20] of String;
    ArrayLength: Integer;
    Low, Mid, High: Integer;
    Result: Integer;
    SearchValue: String;

    Label
    Break;

    Begin
    assign(f, 'filename.txt');
    reset(f);
    i := 0;
    while not eof(f) do
    begin
    readln(f, StringArray[i]);

    write('Enter Person ID to see contact details: ');
    readln(SearchValue);

    inc(i);

    Low := 0;
    High := Length(StringArray[i])-1;
    Result := -1;
    if StringArray[Low] = SearchValue then
    Result := Low
    else if StringArray[High] = SearchValue then
    Result := High
    else while (Low <> High) and (Low+1 <> High) do
    begin
    Mid := (High-Low) div 2 + Low;
    if StringArray[Mid] = SearchValue then
    begin
    Result := Mid;
    Goto Break
    end else if StringArray[Mid] > SearchValue then
    High := Mid
    else
    Low := Mid;
    end;
    if StringArray[Low] = SearchValue then
    Result := Low
    else if StringArray[High] = SearchValue then
    Result := High;
    if Result = -1 then
    writeln(SearchValue+' not found!')
    else
    writeln(SearchValue+' has an index of ',Result);

    end;
    Break:
    Close(f);
    ArrayLength := i;
    End.[/code]
  • : : The Break is necessary to speed up the algorithm. See
    : : http://www.programmersheaven.com/mb/pasprog/370952/371008/ReadMessage
    : : .aspx?S=B20000#371008 for an alternative.
    :
    : Using GoTo hasn't made any difference - the program behaves in the
    : same way if the Break command was not used. And the program does not
    : work. I can type any number equal to or below 1 and "n not found!"
    : is displayed. If I type a number greater than 1 the program consumes
    : a lot of CPU usage.
    :
    : Here's teh code:
    :
    : [code]: Program Search;
    :
    : Uses
    : WinCrt;
    :
    : Var
    : f: Text;
    : i: Integer;
    : StringArray: Array[0..20] of String;
    : ArrayLength: Integer;
    : Low, Mid, High: Integer;
    : Result: Integer;
    : SearchValue: String;
    :
    : Label
    : Break;
    :
    : Begin
    : assign(f, 'filename.txt');
    : reset(f);
    : i := 0;
    : while not eof(f) do
    : begin
    : readln(f, StringArray[i]);
    :
    : write('Enter Person ID to see contact details: ');
    : readln(SearchValue);
    :
    : inc(i);
    :
    : Low := 0;
    : High := Length(StringArray[i])-1;
    : Result := -1;
    : if StringArray[Low] = SearchValue then
    : Result := Low
    : else if StringArray[High] = SearchValue then
    : Result := High
    : else while (Low <> High) and (Low+1 <> High) do
    : begin
    : Mid := (High-Low) div 2 + Low;
    : if StringArray[Mid] = SearchValue then
    : begin
    : Result := Mid;
    : Goto Break
    : end else if StringArray[Mid] > SearchValue then
    : High := Mid
    : else
    : Low := Mid;
    : end;
    : if StringArray[Low] = SearchValue then
    : Result := Low
    : else if StringArray[High] = SearchValue then
    : Result := High;
    : if Result = -1 then
    : writeln(SearchValue+' not found!')
    : else
    : writeln(SearchValue+' has an index of ',Result);
    :
    : end;
    : Break:
    : Close(f);
    : ArrayLength := i;
    : End.[/code]:
    :
    Can you figure out why the program doesn't work? You should be able to since figuring out how the program works is your goal.
  • : Can you figure out why the program doesn't work? You should be able
    : to since figuring out how the program works is your goal.

    i'll try. thanks for all your help :)
Sign In or Register to comment.

Howdy, Stranger!

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

Categories