
-----------------------------------
Fallis15
Mon Jan 14, 2008 6:43 pm

Binary Search Help!
-----------------------------------
I have to do this for school and i need to replace the "search" procedure so that its linear search and not binary search, but even before i can do that i have a problem. its this example i copied down from the workbook, but it doesn't give you the example of the text file to be opened and searched in so i dont know how to set up the text file so it reads properly, i make the name, adress and phone for each student justified so that it has the 30, 30, and 8 characters for each but it still doesnt find the student when i run the program and type in one of the names, any suggestions?

This is the program starting here 
===================================================


% The "BinarySearch" program
% Finds a particular record in a sorted file
% Using the binary search method
type studentType :
    record
        name : string (30)
        address : string (30)
        phone : string (8)
    end record
const maxFile := 500
var studentFile : array 1 .. maxFile of studentType
var count : int


procedure readFile
    % Uses global variables
    var students : int
    open : students , "studentFile.txt", get
    assert students > 0
    count := 0
    loop
        get : students, skip
        exit when eof (students)
        count := count + 1
        get : students, studentFile (count).name : 30,
            studentFile (count).address : 30,
            studentFile (count).phone : 8
    end loop
    put "There are ", count, " students in file"
end readFile


procedure search (studentFile : array 1 .. * of studentType, var key : string (*), count : int, var place : int)
    var first, last, middle : int
    if length (key) = key then
            % Discard last half of list
            last := middle
        else
            % Discard first half of list including middle
            first := middle + 1
        end if
        % exit when only one record left
        exit when first >= last
    end loop

    if studentFile (first).name = key then
        place := first
    else
        place := 0
    end if
end search

readFile
var nameSought : string (30)
loop
    var place : int
    put "Enter name of student to be found (\"stop\" to exit)"
        get nameSought : *
    exit when nameSought = "stop"
    search (studentFile, nameSought, count, place)
    if place not= 0 then
        bind sought to studentFile (place)
        put "name" : 30, "address" : 30, "phone" : 8
        put sought.name : 30, sought.address : 30,
           sought.phone : 8
    else
        put "Not In File!!"
    end if
end loop


======================================================
