Howdy, Stranger!

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

Categories

Pascal Database (Help needed please!)

EluxElux Member Posts: 15
Ok guys, I'm having so much trouble.

I think I've managed to get my search procedures to work but for some reason it won't run can anyone look over me code?

Thanks!

[code]Program RunnerDatabase;

uses crt;

const NAME_CHARS = 30;
NUM_CHARS = 4;

type RunnerRec = record
RunnerNo : string[NUM_CHARS];
Name : string[NAME_CHARS];
FinishTime : longInt;
end;

var RunnerArray : array [0..99] of RunnerRec;
ch : Char;
RecNo : integer;
loopcount : integer;
Hours, Minutes, Seconds, TimeinSeconds : longInt;
runner_num_search, runner_name_search : String;

procedure Runner_Search1(RunnerNo : String);
procedure Runner_Search2(Name : String);

Begin
RunnerArray[0].RunnerNo := '1';
RunnerArray[0].Name := 'Jim';
RunnerArray[0].FinishTime := 3100;
RunnerArray[1].RunnerNo := '2';
RunnerArray[1].Name := 'Dave';
RunnerArray[1].FinishTime := 3120;
RunnerArray[2].RunnerNo := '3';
RunnerArray[2].Name := 'Steven';
RunnerArray[2].FinishTime := 3150;
RecNo := 3;
repeat
clrscr;
gotoxy(15,5);
writeln('Option 1 - Add a new Record');
gotoxy(15,6);
writeln('Option 2 - Sesrch by Runner Number');
gotoxy(15,7);
writeln('Option 3 - Search by Runner Name');
gotoxy(15,8);
writeln('Option 4 - Display complete set of Records');
gotoxy(15,10);
writeln('Q or q to Quit');
gotoxy(15,12);
writeln('Please select an option from the Menu above');
ch:=readkey;

case ch of
'1': Begin
clrscr;
gotoxy(15,5);
writeln('** I N P U T I N G N E W R E C O R D S **');
gotoxy(15,7);
writeln('Please enter the runners number:');
gotoxy(50,7);
readln(RunnerArray[RecNo].RunnerNo);
gotoxy(15,8);
writeln('Please enter runners name:');
gotoxy(50,8);
readln(RunnerArray[RecNo].Name);
gotoxy(15,9);
write('Please enter the finish time using hh mm ss:');
gotoxy(50,9);
readln(Hours);
gotoxy(53,9);
readln(Minutes);
gotoxy(56,9);
readln(Seconds);
TimeinSeconds := Hours * 3600 + Minutes * 60 + Seconds * 1;
RunnerArray[RecNo].FinishTime := TimeinSeconds;
RecNo := RecNo + 1;
end;

'2': Begin
clrscr;
gotoxy(15,5);
writeln('** S E A R C H R E C O R D S **');
writeln('Please enter Runner Number:');
readln(runner_num_search);
Runner_Search1(runner_num_search);
end;

'3': Begin
clrscr;
gotoxy(15,5);
writeln('** S E A R C H R E C O R D S **');
writeln('Please enter Runner Name:');
readln(runner_name_search);
Runner_Search2(runner_name_search);
end;

'4': Begin
clrscr;
gotoxy(8,5);
writeln('** D I S P L A Y C O M P L E T E S E T O F R E C O R D S **');
writeln;
for loopcount := 0 to RecNo-1 do
Begin
write(RunnerArray[loopcount].RunnerNo, ' ');
write(RunnerArray[loopcount].Name, ' ');
Minutes := RunnerArray[loopcount].FinishTime div 60;
Seconds:= RunnerArray[loopcount].FinishTime mod 60;
Hours := Minutes div 60;
Minutes := Minutes mod 60;
writeln(Hours, ':', Minutes, ':', Seconds);
end;
ch:=readkey;
end;

'Q','q': Begin
gotoxy(15,14);
writeln('Press return to Quit');
readln;
end;
end;

until (ch='Q') or (ch='q');
end.[/code]

Comments

  • Phat NatPhat Nat Member Posts: 757
    : Ok guys, I'm having so much trouble.
    :
    : I think I've managed to get my search procedures to work but for some reason it won't run can anyone look over me code?
    :
    : Thanks!
    :
    : [code]Program RunnerDatabase;
    :
    : uses crt;
    :
    : const NAME_CHARS = 30;
    : NUM_CHARS = 4;
    :
    : type RunnerRec = record
    : RunnerNo : string[NUM_CHARS];
    : Name : string[NAME_CHARS];
    : FinishTime : longInt;
    : end;
    :
    : var RunnerArray : array [0..99] of RunnerRec;
    : ch : Char;
    : RecNo : integer;
    : loopcount : integer;
    : Hours, Minutes, Seconds, TimeinSeconds : longInt;
    : runner_num_search, runner_name_search : String;
    :
    [b]: procedure Runner_Search1(RunnerNo : String);
    : procedure Runner_Search2(Name : String);
    [/b]:
    : Begin
    : RunnerArray[0].RunnerNo := '1';
    : RunnerArray[0].Name := 'Jim';
    : RunnerArray[0].FinishTime := 3100;
    : RunnerArray[1].RunnerNo := '2';
    : RunnerArray[1].Name := 'Dave';
    : RunnerArray[1].FinishTime := 3120;
    : RunnerArray[2].RunnerNo := '3';
    : RunnerArray[2].Name := 'Steven';
    : RunnerArray[2].FinishTime := 3150;
    : RecNo := 3;
    : repeat
    : clrscr;
    : gotoxy(15,5);
    : writeln('Option 1 - Add a new Record');
    : gotoxy(15,6);
    : writeln('Option 2 - Sesrch by Runner Number');
    : gotoxy(15,7);
    : writeln('Option 3 - Search by Runner Name');
    : gotoxy(15,8);
    : writeln('Option 4 - Display complete set of Records');
    : gotoxy(15,10);
    : writeln('Q or q to Quit');
    : gotoxy(15,12);
    : writeln('Please select an option from the Menu above');
    : ch:=readkey;
    :
    : case ch of
    : '1': Begin
    : clrscr;
    : gotoxy(15,5);
    : writeln('** I N P U T I N G N E W R E C O R D S **');
    : gotoxy(15,7);
    : writeln('Please enter the runners number:');
    : gotoxy(50,7);
    : readln(RunnerArray[RecNo].RunnerNo);
    : gotoxy(15,8);
    : writeln('Please enter runners name:');
    : gotoxy(50,8);
    : readln(RunnerArray[RecNo].Name);
    : gotoxy(15,9);
    : write('Please enter the finish time using hh mm ss:');
    : gotoxy(50,9);
    : readln(Hours);
    : gotoxy(53,9);
    : readln(Minutes);
    : gotoxy(56,9);
    : readln(Seconds);
    : TimeinSeconds := Hours * 3600 + Minutes * 60 + Seconds * 1;
    : RunnerArray[RecNo].FinishTime := TimeinSeconds;
    : RecNo := RecNo + 1;
    : end;
    :
    : '2': Begin
    : clrscr;
    : gotoxy(15,5);
    : writeln('** S E A R C H R E C O R D S **');
    : writeln('Please enter Runner Number:');
    : readln(runner_num_search);
    : Runner_Search1(runner_num_search);
    : end;
    :
    : '3': Begin
    : clrscr;
    : gotoxy(15,5);
    : writeln('** S E A R C H R E C O R D S **');
    : writeln('Please enter Runner Name:');
    : readln(runner_name_search);
    : Runner_Search2(runner_name_search);
    : end;
    :
    : '4': Begin
    : clrscr;
    : gotoxy(8,5);
    : writeln('** D I S P L A Y C O M P L E T E S E T O F R E C O R D S **');
    : writeln;
    : for loopcount := 0 to RecNo-1 do
    : Begin
    : write(RunnerArray[loopcount].RunnerNo, ' ');
    : write(RunnerArray[loopcount].Name, ' ');
    : Minutes := RunnerArray[loopcount].FinishTime div 60;
    : Seconds:= RunnerArray[loopcount].FinishTime mod 60;
    : Hours := Minutes div 60;
    : Minutes := Minutes mod 60;
    : writeln(Hours, ':', Minutes, ':', Seconds);
    : end;
    : ch:=readkey;
    : end;
    :
    : 'Q','q': Begin
    : gotoxy(15,14);
    : writeln('Press return to Quit');
    : readln;
    : end;
    : end;
    :
    : until (ch='Q') or (ch='q');
    : end.[/code]

    Look at the Bolded area above. You have defined your procedures, but they are not listed anywhere. Therefore, it thinks that these ARE the definitions and it sees a procedure within a procedure without a proper End. This is what the compiler sees:
    [code]
    procedure Runner_Search1(RunnerNo : String);
    procedure Runner_Search2(Name : String);
    Begin
    End.
    [/code]
    when it expects to see:
    [code]
    procedure Runner_Search1(RunnerNo : String);
    procedure Runner_Search2(Name : String);
    Begin
    End;
    Begin
    End;

    Begin
    End.
    [/code]

    You need to write the search code for the procedure. Ex:
    [code]
    procedure Runner_Search1(RunnerNo : String);
    Begin
    { Insert Runner Search Code Here }
    End;

    procedure Runner_Search2(Name : String);
    Begin
    { Insert Runner2 Search Code Here }
    End.

    Begin
    { All your code that you have }
    End.
    [/code]


    Phat Nat

  • EluxElux Member Posts: 15
    Thanks for you reply :)

    I've gone over your help and put this code in for the procedures for it now runs ok but doesnt return a value.

    [code]procedure Runner_Search1(RunnerNo : String);
    var Runner_Search1 : string;
    begin
    Runner_Search1 := runner_num_search;
    end;

    procedure Runner_Search2(Name : String);
    var Runner_Search2 : string;
    begin
    Runner_Search2 := runner_name_search;
    end;[/code]
  • Phat NatPhat Nat Member Posts: 757
    : Thanks for you reply :)
    :
    : I've gone over your help and put this code in for the procedures for it now runs ok but doesnt return a value.
    :
    : [code]procedure Runner_Search1(RunnerNo : String);
    : var Runner_Search1 : string;
    : begin
    : Runner_Search1 := runner_num_search;
    : end;
    :
    : procedure Runner_Search2(Name : String);
    : var Runner_Search2 : string;
    : begin
    : Runner_Search2 := runner_name_search;
    : end;[/code]
    :

    You need to write the search procedures yourself. Right now, it's just an empty procedure. Also, the above code I would expect to return an error because you are trying to use the procedures as functions.
    You need to write a procedure that will search through your array and compare each array entry with the search specs and if they match, return the array number.

    Phat Nat


  • EluxElux Member Posts: 15
    How could I do that because I'm really lost now :S
  • Phat NatPhat Nat Member Posts: 757
    : How could I do that because I'm really lost now :S
    :

    For the first search, you want it to return the array number of the runner with the given Runner#. So to return the info, we will use a FUNCTION instead of a PROCEDURE.

    [code]
    VAR
    SearchResult : Byte; { Holds the array # result of our search }

    FUNCTION Runner_Search1(RunnerNo : String) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search1 := -1;

    { Check all the records for that runner number }
    For Counter := 0 to RecNo-1 Do
    If RunnerArray[Counter] = RunnerNo Then
    Runner_Search1 := Counter;
    end;

    {Main Code}
    Begin
    {...}
    SearchResult := Runner_Search(runner_num_search);
    WriteLn('Runner #',runner_num_search,' is at array record ',SearchResult);
    {...}
    End.
    [/code]

    As you see, we just search through every array record and if we find a match, we return the array record number.
    The name search will be the same, although you may want to convert both names to uppercase before comparing so that names like 'Bob' and 'bob' will be the same.
    This can be done by converting each letter of a string to uppercase using the UPCASE() function. Make a FUNCTION like so to do this:
    [code]
    FUNCTION UpStr(Str : String) : String;
    Begin
    { Insert uppercase code here }
    {...}
    UpStr := Str;
    End;
    [/code]

    Phat Nat

  • EluxElux Member Posts: 15
    [b][red]This message was edited by Elux at 2006-1-21 7:25:17[/red][/b][hr]
    [b][red]This message was edited by Elux at 2006-1-20 16:16:20[/red][/b][hr]
    Thank you for your reply, I've put the code in and it brings back a couple of errors. I'm not sure where the errors are coming from, have I inputted your code in wrong?

    [code]Program RunnerDatabase;

    uses crt;

    const NAME_CHARS = 30;
    NUM_CHARS = 4;

    type RunnerRec = record
    RunnerNo : string[NUM_CHARS];
    RunnerName : string[NAME_CHARS];
    FinishTime : longInt;
    end;

    var RunnerArray : array [0..99] of RunnerRec;
    ch : Char;
    RecNo : integer;
    loopcount : integer;
    Hours, Minutes, Seconds, TimeinSeconds : longInt;
    runner_num_search, runner_name_search : String;
    SearchResult : Byte; { Holds the array # result of our search }

    FUNCTION Runner_Search1(RunnerNo : String) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search1 := -1;

    { Check all the records for that runner number }
    For Counter := 0 to RecNo-1 Do
    If RunnerArray[Counter] = RunnerNo Then
    Runner_Search1 := Counter;
    end;

    FUNCTION Runner_Search2(RunnerNo : String) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search2 := -1;

    { Check all the records for that runner number }
    For Counter := 0 to RecNo-1 Do
    If RunnerArray[Counter] = RunnerName Then
    Runner_Search2 := Counter;
    end;

    Begin
    RunnerArray[0].RunnerNo := '1';
    RunnerArray[0].RunnerName := 'Jim';
    RunnerArray[0].FinishTime := 60;
    RunnerArray[1].RunnerNo := '2';
    RunnerArray[1].RunnerName := 'Dave';
    RunnerArray[1].FinishTime := 120;
    RunnerArray[2].RunnerNo := '3';
    RunnerArray[2].RunnerName := 'Steven';
    RunnerArray[2].FinishTime := 150;
    RecNo := 3;
    repeat
    clrscr;
    gotoxy(15,4);
    writeln('** R U N N E R S D A T A B A S E **');
    gotoxy(15,5);
    writeln('Option 1 - Add a new Record');
    gotoxy(15,6);
    writeln('Option 2 - Sesrch by Runner Number');
    gotoxy(15,7);
    writeln('Option 3 - Search by Runner Name');
    gotoxy(15,8);
    writeln('Option 4 - Display complete set of Records');
    gotoxy(15,10);
    writeln('Q or q to Quit');
    gotoxy(15,12);
    writeln('Please select an option from the Menu above');
    ch:=readkey;

    case ch of
    '1': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** I N P U T I N G N E W R E C O R D S **');
    gotoxy(15,7);
    writeln('Please enter the runners number:');
    gotoxy(50,7);
    readln(RunnerArray[RecNo].RunnerNo);
    gotoxy(15,8);
    writeln('Please enter runners name:');
    gotoxy(50,8);
    readln(RunnerArray[RecNo].RunnerName);
    gotoxy(15,9);
    write('Please enter the finish time using hh mm ss:');
    gotoxy(50,9);
    readln(Hours);
    gotoxy(53,9);
    readln(Minutes);
    gotoxy(56,9);
    readln(Seconds);
    TimeinSeconds := Hours * 3600 + Minutes * 60 + Seconds * 1;
    RunnerArray[RecNo].FinishTime := TimeinSeconds;
    RecNo := RecNo + 1;
    end;

    '2': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** S E A R C H R E C O R D S **');
    writeln('Please enter Runner Number:');
    readln(runner_num_search);
    SearchResult := Runner_Search1(runner_num_search);
    writeln('Runner #',runner_num_search,' is at array record ',SearchResult);
    end;

    '3': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** S E A R C H R E C O R D S **');
    writeln('Please enter Runner Name:');
    readln(runner_name_search);
    SearchResult := Runner_Search2(runner_name_search);
    writeln('Runner #',runner_name_search,' is at array record ',SearchResult);
    end;

    '4': Begin
    clrscr;
    gotoxy(8,5);
    writeln('** D I S P L A Y C O M P L E T E S E T O F R E C O R D S **');
    writeln;
    for loopcount := 0 to RecNo-1 do
    Begin
    write(RunnerArray[loopcount].RunnerNo, ' ');
    write(RunnerArray[loopcount].RunnerName, ' ');
    Minutes := RunnerArray[loopcount].FinishTime div 60;
    Seconds:= RunnerArray[loopcount].FinishTime mod 60;
    Hours := Minutes div 60;
    Minutes := Minutes mod 60;
    writeln(Hours, ':', Minutes, ':', Seconds);
    end;
    ch:=readkey;
    end;

    'Q','q': Begin
    gotoxy(15,14);
    writeln('Press return to Quit');
    readln;
    end;
    end;

    until (ch='Q') or (ch='q');
    end.[/code]





  • EluxElux Member Posts: 15
    [b][red]This message was edited by Elux at 2006-1-23 10:10:24[/red][/b][hr]
    Ok the program now runs and works

    When searching it will say, Runner #1 is at array record 1 or Runner Jim is at array record 1([b]thank you phat nat[/b]), but on the search options I would like to display all records associated with search for runner# and runner name. I'm stuck on that :S

    [code]Program RunnerDatabase;

    uses crt;

    const NAME_CHARS = 30;
    NUM_CHARS = 4;

    type RunnerRec = record
    RunnerNo : string[NUM_CHARS];
    RunnerName : string[NAME_CHARS];
    FinishTime : longInt;
    end;

    var RunnerArray : array [0..99] of RunnerRec;
    ch : Char;
    RecNo : integer;
    loopcount : integer;
    Hours, Minutes, Seconds, TimeinSeconds : longInt;
    runner_num_search, runner_name_search : String;
    SearchResult : Byte; { Holds the array # result of the search }

    FUNCTION Runner_Search1(RunnerNo : String) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search1 := -1;

    { Check all the records for that runner number }
    For Counter := 0 to RecNo-1 Do
    If RunnerArray[Counter].RunnerNo = RunnerNo Then
    Runner_Search1 := Counter;
    end;

    FUNCTION Runner_Search2(RunnerName : String) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search2 := -1;

    { Check all the records for that runner name }
    For Counter := 0 to RecNo-1 Do
    If RunnerArray[Counter].RunnerName = RunnerName Then
    Runner_Search2 := Counter;
    end;

    Begin
    RunnerArray[0].RunnerNo := '1';
    RunnerArray[0].RunnerName := 'Jim';
    RunnerArray[0].FinishTime := 60;
    RunnerArray[1].RunnerNo := '2';
    RunnerArray[1].RunnerName := 'Dave';
    RunnerArray[1].FinishTime := 120;
    RunnerArray[2].RunnerNo := '3';
    RunnerArray[2].RunnerName := 'Steven';
    RunnerArray[2].FinishTime := 150;
    RecNo := 3;
    repeat
    clrscr;
    gotoxy(15,4);
    writeln('** R U N N E R S D A T A B A S E **');
    gotoxy(15,5);
    writeln('Option 1 - Add a new Record');
    gotoxy(15,6);
    writeln('Option 2 - Search by Runner Number');
    gotoxy(15,7);
    writeln('Option 3 - Search by Runner Name');
    gotoxy(15,8);
    writeln('Option 4 - Display complete set of Records');
    gotoxy(15,10);
    writeln('Q or q to Quit');
    gotoxy(15,12);
    writeln('Please select an option from the Menu above');
    ch:=readkey;

    case ch of
    '1': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** I N P U T I N G N E W R E C O R D S **');
    gotoxy(15,7);
    writeln('Please enter the runners number:');
    gotoxy(50,7);
    readln(RunnerArray[RecNo].RunnerNo);
    gotoxy(15,8);
    writeln('Please enter runners name:');
    gotoxy(50,8);
    readln(RunnerArray[RecNo].RunnerName);
    gotoxy(15,9);
    write('Please enter the finish time using hh mm ss:');
    gotoxy(50,9);
    readln(Hours);
    gotoxy(53,9);
    readln(Minutes);
    gotoxy(56,9);
    readln(Seconds);
    TimeinSeconds := Hours * 3600 + Minutes * 60 + Seconds * 1;
    RunnerArray[RecNo].FinishTime := TimeinSeconds;
    RecNo := RecNo + 1;
    end;

    '2': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** S E A R C H R E C O R D S **');
    gotoxy(15,7);
    writeln('Please enter Runner Number:');
    gotoxy(44,7);
    readln(runner_num_search);
    SearchResult := Runner_Search1(runner_num_search);
    gotoxy(15,9);
    writeln('Runner #',runner_num_search,' is at array record ',SearchResult);
    ch:=readkey;
    end;

    '3': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** S E A R C H R E C O R D S **');
    gotoxy(15,7);
    writeln('Please enter Runner Name:');
    gotoxy(44,7);
    readln(runner_name_search);
    SearchResult := Runner_Search2(runner_name_search);
    gotoxy(15,9);
    writeln('Runner ',runner_name_search,' is at array record ',SearchResult)
    ch:=readkey;
    end;

    '4': Begin
    clrscr;
    gotoxy(8,5);
    writeln('** D I S P L A Y C O M P L E T E S E T O F R E C O R D S **');
    writeln;
    for loopcount := 0 to RecNo-1 do
    Begin
    write(RunnerArray[loopcount].RunnerNo, ' ');
    write(RunnerArray[loopcount].RunnerName, ' ');
    Minutes := RunnerArray[loopcount].FinishTime div 60;
    Seconds:= RunnerArray[loopcount].FinishTime mod 60;
    Hours := Minutes div 60;
    Minutes := Minutes mod 60;
    writeln(Hours, ':', Minutes, ':', Seconds);
    end;
    ch:=readkey;
    end;

    'Q','q': Begin
    gotoxy(15,14);
    writeln('Press return to Quit');
    readln;
    end;
    end;

    until (ch='Q') or (ch='q');
    end.[/code]



  • Phat NatPhat Nat Member Posts: 757
    [b][red]This message was edited by Phat Nat at 2006-1-23 16:7:19[/red][/b][hr]
    : Ok the program now runs and works
    :
    : When searching it will say, Runner #1 is at array record 1 or Runner Jim is at array record 1([b]thank you phat nat[/b]), but on the search options I would like to display all records associated with search for runner# and runner name. I'm stuck on that :S
    :

    Well, that's going to require a different setup, since our procedure will only return one result. Let's look at the RunnerNo search:

    [code]
    FUNCTION Runner_Search1(RunnerNo : String) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search1 := -1;

    { Check all the records for that runner number }
    For Counter := 0 to RecNo-1 Do
    If RunnerArray[Counter].RunnerNo = RunnerNo Then
    Runner_Search1 := Counter;
    end;
    [/code]

    First off, good call on finding my mistake. I forgot the record variable.
    So, let's change our procedure to include a start record. This way if we have 12 records, we can tell it to start looking at record 5 (for ex) and search from 5-12.
    [code]
    FUNCTION Runner_Search1(RunnerNo : String[b]; Start : Byte[/b]) : Integer;
    var
    counter : Byte;
    begin
    { If no runners match, then it will return -1 }
    Runner_Search1 := -1;

    { Check all the records for that runner number }
    For Counter := [b]Start[/b] to RecNo-1 Do
    If RunnerArray[Counter].RunnerNo = RunnerNo Then
    [b]Begin[/b]
    Runner_Search1 := Counter;
    [b]Break;
    End;[/b]
    end;
    [/code]

    Notice the changes. We now can specify a starting record and instead of finding the last matching record, it stops when it finds the first.
    I've given you the procedure, but now you have to implement it.

    You need to write code to do the following:
    Make a variable (ex. StartPoint : Integer;)
    If you set a StartPoint to 0, then you can put this into your procedure as a starting search point. Save the result from Runner_Search1 into the StartPoint variable.
    If a record that matches is found, then StartPoint will now point to that array #.
    Now if you keep looping your search until your starting point is equal to -1, then you will be able to access all the records on by one.

    For Example, if we have:
    1 Jim 3100
    2 Dave 3120
    3 Steven 3150
    2 Robert 3200

    and we search for RunnerNo 2 then our first loop will return 1 (Dave) and the next time we search will return 3 (Robert) and finally the third search will return -1 (No More).

    Give it a go,
    Phat Nat




  • EluxElux Member Posts: 15
    [b][red]This message was edited by Elux at 2006-1-24 7:20:1[/red][/b][hr]
    [b]Thank you for your reply Phat Nat[/b]

    I've looked over it and tried to put it in but I think I've done it wrong.
    Will this display all records associated with a runner number or name? For exmaple if I enter runner number 1 will it display all records with the runner number as 1?

    [code]FUNCTION Runner_Search1(RunnerNo : String; Start : Byte) : Integer;
    var
    counter : Byte;
    [b]StartPoint : Integer;[/b]
    begin
    { If no runners match, then it will return -1 }
    Runner_Search1 := -1;
    [b]Startpoint := 0;[/b]


    { Check all the records for that runner number }
    For Counter := [b]StartPoint[/b] to RecNo-1 Do
    If RunnerArray[Counter].RunnerNo = RunnerNo Then
    Begin
    Runner_Search1 := Counter;
    Break;
    End;
    end;[/code]

    [b]Is this how you would call it?[/b]

    [code]'2': Begin
    clrscr;
    gotoxy(15,5);
    writeln('** S E A R C H R E C O R D S **');
    gotoxy(15,7);
    writeln('Please enter Runner Number:');
    gotoxy(44,7);
    readln(runner_num_search);
    [b]StartPoint := Runner_Search1(runner_num_search);[/b]
    gotoxy(15,9);
    write('Runner Number ' ');
    write('Runner Name ' ');
    - FINISH TIME HERE -
    ch:=readkey;
    end;[/code]


  • Phat NatPhat Nat Member Posts: 757
    : I've looked over it and tried to put it in but I think I've done it wrong.
    : Will this display all records associated with a runner number or name? For exmaple if I enter runner number 1 will it display all records with the runner number as 1?
    :
    : [code]FUNCTION Runner_Search1(RunnerNo : String; Start : Byte) : Integer;
    : var
    : counter : Byte;
    : [b]StartPoint : Integer;[/b]
    : begin
    : { If no runners match, then it will return -1 }
    : Runner_Search1 := -1;
    : [b]Startpoint := 0;[/b]
    :
    :
    : { Check all the records for that runner number }
    : For Counter := [b]StartPoint[/b] to RecNo-1 Do
    : If RunnerArray[Counter].RunnerNo = RunnerNo Then
    : Begin
    : Runner_Search1 := Counter;
    : Break;
    : End;
    : end;[/code]
    :
    : [b]Is this how you would call it?[/b]
    :
    : [code]'2': Begin
    : clrscr;
    : gotoxy(15,5);
    : writeln('** S E A R C H R E C O R D S **');
    : gotoxy(15,7);
    : writeln('Please enter Runner Number:');
    : gotoxy(44,7);
    : readln(runner_num_search);
    : [b]StartPoint := Runner_Search1(runner_num_search);[/b]
    : gotoxy(15,9);
    : write('Runner Number ' ');
    : write('Runner Name ' ');
    : - FINISH TIME HERE -
    : ch:=readkey;
    : end;[/code]

    The procedure I gave you was as it needed to be, so don't add the Startpoint variable in there. I want you to come up with the answer yourself as it will help you problem solve other questions. Let me try to clear it up a bit though.

    With the procedure I modified for you, it takes a number as a starting point and it will return the array number of the first matching record.
    Knowing this we can acquire all the matching records.
    If we set our first search point to 0, we know that it will begin searching at the first array record. So, if it returns 3, we know that array records 0,1 & 2 do NOT match, but 3 does.
    Now that we know that record 3 matches, we can display the information for record 3.

    Now if we have 10 records, we only know that 0-2 do not match, but 3 does, so now we can start our search at record 4. If we run our search again, starting at record 4, we may find that our procedure returns 7 as the next matching record. We can then display the information for record 7.

    So far then, we would know that records 0,1,2,4,5 & 6 do not match and that 3 & 7 do (and we have already displayed the information for them).
    Now we can run our search again, starting at our last match+1 (in this case 8). If 8 matches then we can display the info for record 8 and start our next search at that match+1 (in this case 9).

    If records 9 & 10 do NOT match, then our procedure will return -1 and we know that there are no more matches.

    So, we can simplifiy it to this:
    1) Set our search starting point to 0
    2) Run our search
    3) If result is -1 then goto step #7
    4) display information for record[result]
    5) set our search starting point to result
    6) goto step #2
    7) all the records have been displayed

    Hope this clears things up a bit,
    Phat Nat

Sign In or Register to comment.