Docsity

CS 2073 Sequential Search Example: Inserting License Numbers into an Array, Study notes of Computer Science

Toggle sidebar
bg1
CS 2073, Example of Sequential Search, Mon Mar 26 2001, 03:55:04 CST
program insertion;
(*------------------------------------------------------------------
* Name: Neal R. Wagner
* Date: March 26, 1992
* Course: CS 2073, Section 001
*------------------------------------------------------------------
* Program: Insert license numbers into an array L.
* (Assumes license numbers are just integers
*------------------------------------------------------------------
* Input: A file of old license numbers, LIC.DAT, internally Lfile
* Output: The same file, updated
*------------------------------------------------------------------
*)
const MAXL = 10; (* max number of license numbers *)
type Ltype = array[1..MAXL] of integer; (* the main array *)
var
L: Ltype; (* the main array *)
numL: integer; (* current number of license numbers *)
Lfile: text; (* the file of license numbers *)
X: integer; (* new license number to lookup *)
function lookup (L: Ltype; X: integer; numL: integer): integer;
(* loop up license number X in array L *)
var i: integer;
begin
lookup := 0;
for i := 1 to numL do
if L[i] = X then lookup := i
end; (* lookup *)
procedure insert(var L: Ltype; X: integer; var numL: integer);
(* insert new license number X in array L *)
(* ASSUMES X NOT ALREADY PRESENT *)
begin
if numL = MAXL then writeln(’*** overflow ***’)
else
begin
numL := numL + 1;
L[numL] := X
end
end; (* insert *)
procedure loadL(var Lfile: text; var L: Ltype; var numL: integer);
(* read from file LIC.DAT into L *)
begin
reset(Lfile, ’LIC.DAT’);
numL := 0;
while not eof(Lfile) and (numL < MAXL) do
begin
numL := numL + 1;
readln(Lfile, L[numL])
end
end; (* loadL *)
procedure dumpL(var Lfile: text; L: Ltype; numL: integer);
(* write array L into file LIC.DAT *)
var i: integer;
begin
rewrite(Lfile, ’LIC.DAT’); writeln(’*** dummping L ***’);
for i := 1 to numL do
begin
writeln(Lfile, L[i]);
writeln(L[i])
end;
close(Lfile)
end; (* dumpL *)
begin
loadL(Lfile, L, numL);
writeln(’Please enter new license #’);
readln(X);
while X <> 0 do
begin
if lookup(L, X, numL) > 0 then
writeln(’Tow the car with license:’, X)
Page 1
pf2

Partial preview of the text

Download CS 2073 Sequential Search Example: Inserting License Numbers into an Array and more Study notes Computer Science in PDF only on Docsity!

CS 2073, Example of Sequential Search, Mon Mar 26 2001, 03:55:04 CST

program insertion; (*------------------------------------------------------------------

  • Name: Neal R. Wagner
  • Date: March 26, 1992
  • Course: CS 2073, Section 001 *------------------------------------------------------------------
  • Program: Insert license numbers into an array L.
  • (Assumes license numbers are just integers *------------------------------------------------------------------
  • Input: A file of old license numbers, LIC.DAT, internally Lfile
  • Output: The same file, updated *------------------------------------------------------------------ ) const MAXL = 10; ( max number of license numbers ) type Ltype = array[1..MAXL] of integer; ( the main array ) var L: Ltype; ( the main array ) numL: integer; ( current number of license numbers ) Lfile: text; ( the file of license numbers ) X: integer; ( new license number to lookup *)

function lookup (L: Ltype; X: integer; numL: integer): integer; (* loop up license number X in array L ) var i: integer; begin lookup := 0; for i := 1 to numL do if L[i] = X then lookup := i end; ( lookup *)

procedure insert(var L: Ltype; X: integer; var numL: integer); (* insert new license number X in array L ) ( ASSUMES X NOT ALREADY PRESENT ) begin if numL = MAXL then writeln(’** overflow **’) else begin numL := numL + 1; L[numL] := X end end; ( insert *)

procedure loadL(var Lfile: text; var L: Ltype; var numL: integer); (* read from file LIC.DAT into L ) begin reset(Lfile, ’LIC.DAT’); numL := 0; while not eof(Lfile) and (numL < MAXL) do begin numL := numL + 1; readln(Lfile, L[numL]) end end; ( loadL *)

procedure dumpL(var Lfile: text; L: Ltype; numL: integer); (* write array L into file LIC.DAT ) var i: integer; begin rewrite(Lfile, ’LIC.DAT’); writeln(’** dummping L **’); for i := 1 to numL do begin writeln(Lfile, L[i]); writeln(L[i]) end; close(Lfile) end; ( dumpL *)

begin loadL(Lfile, L, numL); writeln(’Please enter new license #’); readln(X); while X <> 0 do begin if lookup(L, X, numL) > 0 then writeln(’Tow the car with license:’, X)

Page 1

CS 2073, Example of Sequential Search, Mon Mar 26 2001, 03:55:04 CST

else begin insert(L, X, numL); writeln(’Give a warning to car with license:’, X) end; readln(X) end; dumpL(Lfile, L, numL) end. INITIAL RUN: Please enter new license # 47 Give a warning to car with license: 47 23 Give a warning to car with license: 23 0 *** dummping L *** 47 23 SECOND RUN: Please enter new license # 56 Give a warning to car with license: 56 47 Tow the car with license: 47 23 Tow the car with license: 23 56 Tow the car with license: 56 0 *** dummping L *** 47 23 56 THIRD RUN: Please enter new license # 89 Give a warning to car with license: 89 56 Tow the car with license: 56 77 Give a warning to car with license: 77 23 Tow the car with license: 23 33 Give a warning to car with license: 33 99 Give a warning to car with license: 99 61 Give a warning to car with license: 61 19 Give a warning to car with license: 19 21 Give a warning to car with license: 21 39 *** overflow *** Give a warning to car with license: 39 0 *** dummping L *** 47 23 56 89 77 33 99 61 19 21

Page 2