Intro to Computer Science I (Pascal Software Design Work from Spring 1992)
========================================
BASIC INFORMATION:
Name:______________________________
Course: 266-256 Intro. Comp. Sci. I
Section:_____
Instructor: L. Curl
Assignment:________________________
Date:______________________________
PROBLEM STATEMENT:
{The purpose of this program is to demonstrate terminal input
and printed output and simple arithmetic in Pascal as well
as to provide familiarity with the VAX editor and DCL.}
INPUT/OUTPUT:
Input Data -
{Input is from the keyboard. Integers representing the beginning and
ending water meter readings are entered.}
Output Requirements -
{The output consists of the gallons of water use, the rate charged per
gallon for water use and the total charge for water used. The output
is in the form of a casual table with $ and decimals aligned.
CONSTANTS
RATE = 0.0192; {COST PER GALLON OF WATER}
VARIABLES:
METERST:INTEGER; {INITIAL METER READING, IN GALLONS}
METERND:REAL; {FINAL METER READING, IN GALLONS}
GALUSED:REAL; {FINAL READING - INITIAL READING}
COST : REAL; {GALUSED * RATE}
ALGORITHM DESCRIPTION:
The algorithm consists of a single main program with no procedure modules.
The commands are executed sequentially with no branches or loops.
1. The beginning and ending meter readings are input data values
entered by the user at the keyboard.
2. The total cost is calculated as the product of the rate and
the number of gallons used. The rate is a constant value.
3. Output consists of the values for gallons used, rate, and
total cost printed in a table.
========================================
BASIC INFORMATION:
Course: 266-256 Intro. Comp. Sci. I
Section: 001
Instructor: Linda Curl
Assignment: Program 2
Date: 02/24/92
PROBLEM STATEMENT:
{
The purpose of this program is to demonstrate the use of internal
procedures to construct the solution to an extended version of
programming problem #1.
}
INPUT/OUTPUT:
Input Data -
{Input is from the keyboard: an integer account number, a character user
type, integer values for the beginning and ending water meter readings.
The user at the keyboard enters a real type value for miscellaneous charges.}
Output Requirements -
{The output is in the form of one printed bill which will show:
All of the information provided during the Input stage, and
additional information regarding the water rate and the
total balance due. }
VARIABLES:
ACCOUNT : INTEGER; {customer ID number}
USERTYPE : CHAR; {Residential, Commerical or, Industrial}
METERST : INTEGER; {beginning meter reading}
METERND : INTEGER; {ending meter reading}
MISC : REAL; {miscellaneous charges}
WATERUSED: INTEGER; {cu ft water used}
WATERRATE: REAL; {dollars/cu ft charged for water}
WATERCOST: REAL; {rate * cu ft used}
SEWERUSED: INTEGER; {calculated as 130% of water use}
SEWERRATE: REAL; {calculated as 90% of water rate}
SEWERCOST: REAL; {rate * use}
TOTALCOST: REAL; {sum of sewer and water charges}
ALGORITHM DESCRIPTION:
The main program algorithm sequentially calls 5 procedure modules.
The procedures are all internal to Program2 and are executed in this
order: ReadData, CalcRate, CalcChg, CalcTot, and PrintOut. All variables
are global and no parameters are passed to procedures.
*************
Procedure ReadData;
PURPOSE- {Let user at keyboard type in data values for program}
INPUT- user at keyboard enters:
OUTPUT- none
ALGORITHM- 1. An internal procedure that calls no other procedures
2. Executed sequentially with no branches or loops.
3. Consists of....
Procedure CalcRate;
PURPOSE- {Calculate rate for water based amount and type of use}
INPUT- uses values assigned in ReadData for type, and for meter
starting and ending readings.
OUTPUT-rate of charge for water use in dollars per cubic foot.
ALGORITHM- 1. Calls no other procedures.
2. Uses a branching technique to assign a value to
the water rate based on type and amount of use.
Procedure CalcChg;
PURPOSE- {Compute total charge for water and sewer}
INPUT- uses water rate from CalcRate as well as defined constants
to calculate charges for sewer and for water.
OUTPUT-costs for both water and sewer
ALGORITHM- 1. Computes the charge for water as the product of the
rate and the amount used.
2. Computes the charge for sewer as a percent of the
water rate times a percent of the water use.
Procedure CalcTot;
PURPOSE- {Calculate the total charge for sewer and water}
INPUT- water, sewer and miscellaneous charges
OUTPUT- total balance due
ALGORITHM - Calculates the sum of sewer charges and water charges.
Procedure PrintOut;
PURPOSE- {Print individual account bills on paper}
INPUT- all values, both input and computed
OUTPUT- a display of input values, and costs with a balance due
{There is a heading for each bill. Selected values
are printed in the form of a customer bill. Each
item in the bill has a descriptive label. Decimals
and $ are aligned.}
ALGORITHM- 1. Consists of a sequence of Writeln statements.
ERROR TRAPPING: If a user inputs anything other than R, C, or I for user type,
the message THIS USER TYPE IS NOT RECOGNIZED will appear on
the output...This indicates an error in the input process
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course:266-256 Introduction to Computer Science I
Section: 001
Instructor: Linda Curl
Assignment: PROGRAM3
Date: 03/02/92
PROBLEM STATEMENT:
{
The purpose of this program is to demonstrate the techniques
needed for reading data from files in a Pascal program and
dealing with conditional loops such as the WHILE loop.
}
INPUT/OUTPUT:
Input Data - Input is entirely from a datafile. The file consists of an
undetermined number of lines of data in the format:
aaaaT ssssss eeeeee mmmmm
Where aaaa is a four digit account number,
T is a single letter representing the user type (R,C, or I),
sssss is the starting meter reading in exponential notation,
eeeee is the ending meter reading in exponential notation,
mmmmm is the miscellaneous charge in dollars.
Output Requirements - The output is to be in the form of a table of
values including:
Account Number
Account Type
Beginning Meter Reading
Ending Meter Reading
Rate for Water Use
Water Charge
Sewer Charge
Miscellaneous Charges
Balance Due
Also, a Grand Total of all balances due will be printed
at the end of the list.
VARIABLES:
CONST
RESRAT = 0.0192 ; (* Residential Water Rate *)
COMRT1 = 0.0003 ; (* Commercial Rate For 4 Million cu ft Or Less *)
COMRT2 = 0.00025 ; (* Commercial Rate For Greater Than 4 Million cu ft *)
INDRT1 = 0.00025 ; (* Industrial Rate For 4 Million cu ft Or Less *)
INDRT2 = 0.0002 ; (* Industrial Rate For Usage Greater that 4 Million cu ft
And Less Than Or Equal To 10 Million cu ft *)
INDRT3 = 0.00015; (* Industrial Rate For Greater Than 10 Million cu ft *)
VAR
LAB3DATA : TEXT; (* File to be read to get all input data *)
GRANDTOT : REAL; (* GRAND TOTAL accumulated thoughout the WHILE loop *)
ACCOUNT : INTEGER; (* customer ID (account) number *)
USERTYPE : CHAR; (* 'R'esidential, 'C'ommerical or, 'I'ndustrial *)
METERST : REAL; (* beginning meter reading in cu ft *)
METERND : REAL; (* ending meter reading in cu ft *)
MISC : REAL; (* miscellaneous charges *)
WATERUSED: REAL; (* (computed) cu ft water used *)
WATERRATE: REAL; (* dollars/cu ft charged for water *)
WATERCOST: REAL; (* rate * cu ft used *)
SEWERUSED: REAL; (* calculated as 130% of water use *)
SEWERRATE: REAL; (* calculated as 90% of water rate *)
SEWERCOST: REAL; (* rate * use *)
TOTALCOST: REAL; (* sum of sewer and water charges *)
ALGORITHM DESCRIPTION:
1. A module to write a table heading is called prior to entering
the data processing loop so that the heading is not written repeatedly
2. The main program algorithm calls a series of procedure modules
repeatedly in a WHILE loop so that the contents of a datafile can be
read and used for calculations and output.
3. The grand total balance due is written after the loop so that it is
not written repeatedly
{The procedures are all internal to Program3 and are described below.}
Procedure Heading;
PURPOSE- To print a heading at the top of the list of output
values that describes what values are printed in that column.
INPUT- NONE
OUTPUT- A two line heading.
ALGORITHM- The main program will call this procedure before initiating
the WHILE loop. This will make sure that the heading is
only printed once at the top of the page.
Procedure ReadData;
PURPOSE- To retrieve data from a file
INPUT- All input is from a file call LAB3DATA.DAT
OUTPUT- The Variables ACCOUNT, USERTYPE, METERST, METERND, and MISC
will be set to values indicated be the input from the file.
ALGORITHM- This procedure is called repeatedly by the WHILE loop
is the main program. All inputs will be used for the
additional computations and other procedures described
in the main program routine.
Procedure CalcRate;
PURPOSE- {Calculate rate for water based amount and type of use}
INPUT- uses values assigned in ReadData for type, and for meter
starting and ending readings.
OUTPUT-rate of charge for water use in dollars per cubic foot.
ALGORITHM- 1. Calls no other procedures.
2. Uses a branching technique to assign a value to
the water rate based on type and amount of use.
Procedure CalcChg;
PURPOSE- {Compute total charge for water and sewer}
INPUT- uses water rate from CalcRate as well as defined constants
to calculate charges for sewer and for water.
OUTPUT-costs for both water and sewer
ALGORITHM- 1. Computes the charge for water as the product of the
rate and the amount used.
2. Computes the charge for sewer as a percent of the
water rate times a percent of the water use.
Procedure CalcTot;
PURPOSE- {Calculate the total charge for sewer and water}
INPUT- water, sewer and miscellaneous charges
OUTPUT- total balance due
ALGORITHM - Calculates the sum of sewer charges and water charges.
Procedure PrintOut;
PURPOSE- {Print individual account bills on paper}
INPUT- all values, both input and computed
OUTPUT- a display of input values, and costs with a balance due
{This procedure is used to display a single line of
number on the list of values. It is called repeatedly
by the main program routine to provide a complete list
of all values that need to be printed for every
account in the LAB3DATA file }
ALGORITHM- 1. Consists of a sequence of Write/Writeln statements.
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course: 266-256-1
Section: 001
Instructor: L. Curl
Assignment: Program 4
Date: 03/23/92
PROBLEM STATEMENT:
{
The purpose of this program is to demonstrate the techniques
needed to use external procedures and functions and pass
parameters back and forth with these subprograms. The program reads
a datafile and produces a series of printed tables and statements
using a file of data concerning students at WildWood High School
}
INPUT/OUTPUT:
Input Data -
{ All of the data that the program will use as input will be from
a file called LAB4DATA.DAT. The program will use Afile as a
variable name to read the file using type TEXT. The procedure will
use DataFile as a variable name to access the same file. }
Output Requirements-
The output is to be in several forms: a numbered listing of the output
that will be produced by the program, followed by the output itself:
1. a table of columns including all values read in from the file.
2. a table of columns including all values read in from the file for
students with a GPA at or over 2.75 and height between 5'10" and 6'10".
3. A sentence printing the average GPA for the entire student body.
4. A sentence printing the id number, height, weight, and GPA for the
tallest student in the file.
VARIABLES:
Afile : Text; {Each line of this file contains student data}
GPA : Real; {Grade point average for one student}
GradePt : Real; {Average grade point of all students}
Height : Integer; {Height in inches}
Weight : Integer; {Weight in pounds}
StuID : Integer; {Student ID number}
ALGORITHM DESCRIPTION:
The main program prints a series of descriptive remarks and calls
3 external procedures and 1 external function in sequence.
Procedure ListData(Var Afile : Text);
PURPOSE-
{This procedure will print a columnar table consisting of all the
information for each student in Afile.}
PARAMETERS/VARIABLES-
Variable parameters- var DataFile : text; {same as Afile in main prog}
Value parameters- None
Local Variables- GPA : Real {The local varibles will have the}
Height : Integer {same meaning as the corresponding}
Weight : Integer {ones in the main program.}
StuID : Integer
INPUT- All input will come from the file LAB4DATA.DAT which the
main program will refer to as Afile, and this procedure will
refer to as DataFile.
OUTPUT- This procedure will display all of the student information
in LAB4DATA. The data will be display in columns under a
heading with rows of dashes to make it look good.
ALGORITHM-
1. reset the datafile
2. print a table heading and column headings
call procedure Dashes to write a line of 80 dashes for table
3. loop to repeat reading and printing file data
Procedure FindTall(Var Afile : Text);
PURPOSE-
{This procedure will list all information for students in Afile
whose GPA is at or over 2.75 and whose height is between 5'10" and 6'10".
PARAMETERS/VARIABLES-
Variable parameters- var DataFile : text;{Same as Afile in main prog.}
Value parameters- None
Local Variables- StuID : Integer {These local variables will have}
Height : Integer {the same meaning as the}
Weight : Integer {corresponding ones in the main}
GPA : Integer {program.}
INPUT- All input will come from the file LAB4DATA.DAT which the
main program will refer to as Afile, and this procedure will
refer to as DataFile.
OUTPUT- This procedure will display the data of only those students
in the file that are between 70 (Inclusive) and 82 (Inclusive)
inches tall and have a GPA of at least 2.75.
ALGORITHM-
1. reset the datafile
2. use a WHILE loop to go thru all the students in the file
3. within the loop, read data on a student
4 check GPA and height. If it is within the parameters specified
above, display data on that student.
Function AvgGPA(Var Afile : Text) : Real;
{This function calculates and returns the average GPA of
all students listed in Afile.}
PURPOSE-
PARAMETERS/VARIABLES-
Variable parameters- var Afile : text;
Value parameters-
Local Variables- StuID : Integer {These local variables will have}
Height : Integer {the same meaning as the}
Weight : Integer {corresponding ones in the main}
GPA : Integer {program.}
Total : Real {total of all GPA's (sum)}
Count : Integer {Used to count number of students}
INPUT- All input will come from the file LAB4DATA.DAT which the
main program will refer to as Afile, and this procedure will
refer to as DataFile.
OUTPUT- The only output this procedure will perform is to return the
average GPA of all students in the file as the function AvgGPA.
ALGORITHM-
1. reset the datafile
2. reset COUNT and TOTAL variables
3. use a WHILE loop to go thru all of the students in the file
4. within the loop, get student data and increment COUNT by 1
and TOTAL by the student GPA.
5. after the loop, divide the TOTAL by COUNT and assign the result
to be the value that the function will return.
Procedure TallestStu (Var Afile : Text;
Var StuID : Integer;
Var Height : Integer;
Var Weight : Integer;
Var GPA : Real);
{This procedure locates the data for the tallest student and
returns the data to the main program for printing}
PARAMETERS/VARIABLES-
Variable parameters- var DataFile : Text
var StuID : Integer
var Height : Integer
var Weight : Integer
var GPA : Integer
Value parameters- NONE
Local Variables- Highest : Integer {Used in loop for greatest height}
RStuID : Integer {Temporary StuID}
RHeight : Integer {Temporary Height}
RWeight : Integer {Temporary Weight}
RGPA : Real {Temporary GPA}
{ The Temporary local variables listed above exist
only to read the data so that the corresponding variable
parameters will not be affected by the READLN command in
cases where the data being read does not correspond the
the tallest student }
INPUT- All input will come from the file LAB4DATA.DAT which the
main program will refer to as Afile, and this procedure will
refer to as DataFile.
OUTPUT- The variables StuID,Height,Weight, and GPA will be set to the
values corresponding with the tallest student in the file.
These changes will also be affected at the main program's
level so the output of this procedure can be used as input
for the main program.
ALGORITHM-
1. reset the datafile
2. Set the local variable Highest to a lower bound, in this case 0.
2. use a WHILE loop the go thru all of the students in the file
3. read the data for the student into the temporary variables
4. if the hight read exceeds the Highest so far, store all of the
data pertaining to that student into the variable parameters
Procedure Dashes;
PURPOSE- {Prints line of dashes for reports, etc.}
PARAMETERS - NONE
LOCAL VARIABLES - I : INTEGER {used in a loop to repeat dashes}
INPUT- NONE
OUTPUT- printed line of dashes
ALGORITHM- 1. Initialize a FOR loop { 1 thru 80 }
2. Display a single dash without Return or Linefeed
TESTING:
Create a separate driver for each procedure.
Drivers should include:
1. Display beginning of driver
2. Use of the procedure
3. Display end of driver
The drivers will consist of a copy of the main program with all of the
other procedure calls converted into comments or otherwise made inoperative.
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course: 266-256
Section: 001
Instructor: L.Curl
Assignment: Program 5
Date: 04/01/92
PROBLEM STATEMENT:
{
The purpose of this program is to demonstrate branching with the
CASE statement and looping with REPEAT/UNTIL as well as
interactive user control of program execution via a menu of selections.
}
INPUT/OUTPUT:
Input Data -
{Input is partially from a datafile. The file consists of an
undetermined number of lines of data in the format:
STUDENT ID HEIGHT(INCHES) WEIGHT(LBS) GPA
Menu selection input is obtained from the user at the keyboard in
the form of numbers 1 thru 5.}
Output Requirements-
{The output is to be in several forms:
1. A list of a student data in the file.
2. Display the Average GPA
3. Display list of students over 72 inches tall with above
average GPA.
4. Display the data on the tallest student.
VARIABLES:
Afile : Text; {Each line of this file contains student data}
Average : Real; {Average GPA for all students in Afile}
Number : Integer; {Number of students listed}
StuID : Integer; {Student ID Number}
Height : Integer; {Student Height in Inches}
Weight : Integer; {Student Weight in Pounds}
GPA : Real; {Student GPA}
Selection : Char; {Menu option selected by user}
ALGORITHM DESCRIPTION:
The main program uses a Repeat/Until loop to repeatedly display a menu of
available options (procedure Showmenu), get the user's choice of options
(procedure GetChoice), and activate the selected procedure or function
until the user selects option Exit. The main program then leaves the
loop, prints exit messages, and stops.
Procedure Showmenu; {Displays a menu}
PURPOSE- To display a menu of selections
PARAMETERS/VARIABLES-
Variable parameters- NONE
Value parameters- NONE
Local Variables- NONE
OUTPUT- A display of a menu of available selections
ALGORITHM- Displays a menu
Procedure GetChoice(var Selection:char); {Get selection from user}
PURPOSE- To prompt the user for input and accept input
PARAMETERS/VARIABLES-
Variable parameters- Selection:char {Character entered by user}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a Prompt
ALGORITHM- Displays a prompt and accepts input (stored in Selection)
Procedure ListData(Var Afile : Text); {Displays data on all students}
PURPOSE-
{This procedure will print a columnar table consisting of all the
information for each student in Afile.
This procedure has an internal procedure called Dashes that
displays a row of dashes.}
PARAMETERS/VARIABLES-
Variable parameters- var DataFile : text;
Value parameters- NONE
Local Variables- GPA : Real; {Grade point average for one student}
Height : Integer; {Height of student in inches}
Weight : Integer; {Weight of student in pounds}
StuID : Integer; {Student ID number}
OUTPUT- Displays a heading and all student data in DataFile
ALGORITHM-
1. reset the datafile
2. print a table heading and column headings
call procedure Dashes to write a line of 80 dashes for table
3. loop to repeat reading and printing file data
PROCEDURE FindTall (var DataFile : Text; {Afile from main program}
Average : Real; {Average GPA of all students}
var Number : Integer {Counts number of students} );
PURPOSE- Displays the data of the students with both an above average
GPA and a height of at least 72 inches.
PARAMETERS/VARIABLES-
Variable parameters- DataFile : Text {Afile from main program}
Number : Integer {Counts number of students}
Value parameters- Average : Real {Average GPA of all students}
Local Variables- StuID, Height, Weight : Integer; {Student data - same meaning as}
GPA : Real; {corresponding variables in main.}
OUTPUT- Displays the data of the students with both a height over 72
inches and a GPA greater that the average GPA of the whole file.
ALGORITHM- 1. Reset DataFile, Display Heading, Set Number to 0.
2. Use a WHILE loop to go thru all the students in the file.
3. Compare the height to 72, and GPA to Average.
4. IF both are greater, display the data on that student
and increment Number.
Function AvgGPA(Var DataFile : Text) : Real; {Calculate the average GPA}
PURPOSE- Return the value of the average GPA of the whole file.
PARAMETERS/VARIABLES-
Variable parameters-DataFile : Text; {Corresponds to Afile}
Value parameters- NONE
Local Variables- Count : Integer; {Used as a counter to find the number of students}
Total : Real; {Used to accumulate the total of all GPA's}
StuID, Height, Weight : Integer; {Student data : not used here}
GPA : Real; {GPA for each student : Read in from file}
OUTPUT- Return the average GPA of all students in the file.
ALGORITHM- 1. Reset DataFile, Set Count and Total to 0.
2. Setup a WHILE loop to go thru all the students in the file.
3. Read the data on the student.
4. Increment Count by 1.
5. Increment Total by the student's GPA.
6. After the loop, assign Total/Count to the AvgGPA function.
Procedure TallestStu( Var DataFile : Text;
Var StuID, Height, Weight : Integer;
Var GPA : Real ); {variable parameters}
{This procedure provides the data corresponding to the tallest student}
PURPOSE- To return variable data to the main program regarding the
tallest student in the file.
PARAMETERS/VARIABLES-
Variable parameters- Var DataFile : Text;
Var StuID, Height, Weight : Integer;
Var GPA : Real ;
Value parameters- NONE
Local Variables- Highest : Integer {Used to find the tallest student}
RStuID,RHeight,RWeight : Integer ; {These varibles are used to read}
RGPA : Real ; {from the data file}
OUTPUT- Returns variable data to the main program regarding the
tallest student.
ALGORITHM-
1. Reset DataFile, Set Highest to a lower bound (0).
2. Setup a loop to go thru the file.
3. Read Student data.
4. If the students height is greater that the highest
read so far, assign the students data to the variables
which are to be returned to the main program.
TESTING:
Create a stubs module to test the main program.
Stubs should include:
1. Every procedure and function in the regular module in a
stripped down, minimally function version.
2. A display of the activation of each stub.
3. Variables will be set to test values to determine the
effectiveness of the main program.
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course: 266-256
Section: 001
Instructor: L.Curl
Assignment: Program 5
Date: 04/01/92
PROBLEM STATEMENT:
{
The purpose of this program is to demonstrate branching with the
CASE statement and looping with REPEAT/UNTIL as well as
interactive user control of program execution via a menu of selections.
}
INPUT/OUTPUT:
Input Data -
{Input is partially from a datafile. The file consists of an
undetermined number of lines of data in the format:
STUDENT ID HEIGHT(INCHES) WEIGHT(LBS) GPA
Menu selection input is obtained from the user at the keyboard in
the form of numbers 1 thru 5.}
Output Requirements-
{The output is to be in several forms:
1. A list of a student data in the file.
2. Display the Average GPA
3. Display list of students over 72 inches tall with above
average GPA.
4. Display the data on the tallest student.
VARIABLES:
Afile : Text; {Each line of this file contains student data}
Average : Real; {Average GPA for all students in Afile}
Number : Integer; {Number of students listed}
StuID : Integer; {Student ID Number}
Height : Integer; {Student Height in Inches}
Weight : Integer; {Student Weight in Pounds}
GPA : Real; {Student GPA}
Selection : Char; {Menu option selected by user}
ALGORITHM DESCRIPTION:
The main program uses a Repeat/Until loop to repeatedly display a menu of
available options (procedure Showmenu), get the user's choice of options
(procedure GetChoice), and activate the selected procedure or function
until the user selects option Exit. The main program then leaves the
loop, prints exit messages, and stops.
Procedure Showmenu; {Displays a menu}
PURPOSE- To display a menu of selections
PARAMETERS/VARIABLES-
Variable parameters- NONE
Value parameters- NONE
Local Variables- NONE
OUTPUT- A display of a menu of available selections
ALGORITHM- Displays a menu
Procedure GetChoice(var Selection:char); {Get selection from user}
PURPOSE- To prompt the user for input and accept input
PARAMETERS/VARIABLES-
Variable parameters- Selection:char {Character entered by user}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a Prompt
ALGORITHM- Displays a prompt and accepts input (stored in Selection)
Procedure ListData(Var Afile : Text); {Displays data on all students}
PURPOSE-
{This procedure will print a columnar table consisting of all the
information for each student in Afile.
This procedure has an internal procedure called Dashes that
displays a row of dashes.}
PARAMETERS/VARIABLES-
Variable parameters- var DataFile : text;
Value parameters- NONE
Local Variables- GPA : Real; {Grade point average for one student}
Height : Integer; {Height of student in inches}
Weight : Integer; {Weight of student in pounds}
StuID : Integer; {Student ID number}
OUTPUT- Displays a heading and all student data in DataFile
ALGORITHM-
1. reset the datafile
2. print a table heading and column headings
call procedure Dashes to write a line of 80 dashes for table
3. loop to repeat reading and printing file data
PROCEDURE FindTall (var DataFile : Text; {Afile from main program}
Average : Real; {Average GPA of all students}
var Number : Integer {Counts number of students} );
PURPOSE- Displays the data of the students with both an above average
GPA and a height of at least 72 inches.
PARAMETERS/VARIABLES-
Variable parameters- DataFile : Text {Afile from main program}
Number : Integer {Counts number of students}
Value parameters- Average : Real {Average GPA of all students}
Local Variables- StuID, Height, Weight : Integer; {Student data - same meaning as}
GPA : Real; {corresponding variables in main.}
OUTPUT- Displays the data of the students with both a height over 72
inches and a GPA greater that the average GPA of the whole file.
ALGORITHM- 1. Reset DataFile, Display Heading, Set Number to 0.
2. Use a WHILE loop to go thru all the students in the file.
3. Compare the height to 72, and GPA to Average.
4. IF both are greater, display the data on that student
and increment Number.
Function AvgGPA(Var DataFile : Text) : Real; {Calculate the average GPA}
PURPOSE- Return the value of the average GPA of the whole file.
PARAMETERS/VARIABLES-
Variable parameters-DataFile : Text; {Corresponds to Afile}
Value parameters- NONE
Local Variables- Count : Integer; {Used as a counter to find the number of students}
Total : Real; {Used to accumulate the total of all GPA's}
StuID, Height, Weight : Integer; {Student data : not used here}
GPA : Real; {GPA for each student : Read in from file}
OUTPUT- Return the average GPA of all students in the file.
ALGORITHM- 1. Reset DataFile, Set Count and Total to 0.
2. Setup a WHILE loop to go thru all the students in the file.
3. Read the data on the student.
4. Increment Count by 1.
5. Increment Total by the student's GPA.
6. After the loop, assign Total/Count to the AvgGPA function.
Procedure TallestStu( Var DataFile : Text;
Var StuID, Height, Weight : Integer;
Var GPA : Real ); {variable parameters}
{This procedure provides the data corresponding to the tallest student}
PURPOSE- To return variable data to the main program regarding the
tallest student in the file.
PARAMETERS/VARIABLES-
Variable parameters- Var DataFile : Text;
Var StuID, Height, Weight : Integer;
Var GPA : Real ;
Value parameters- NONE
Local Variables- Highest : Integer {Used to find the tallest student}
RStuID,RHeight,RWeight : Integer ; {These varibles are used to read}
RGPA : Real ; {from the data file}
OUTPUT- Returns variable data to the main program regarding the
tallest student.
ALGORITHM-
1. Reset DataFile, Set Highest to a lower bound (0).
2. Setup a loop to go thru the file.
3. Read Student data.
4. If the students height is greater that the highest
read so far, assign the students data to the variables
which are to be returned to the main program.
TESTING:
Create a stubs module to test the main program.
Stubs should include:
1. Every procedure and function in the regular module in a
stripped down, minimally function version.
2. A display of the activation of each stub.
3. Variables will be set to test values to determine the
effectiveness of the main program.
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course: 266-256
Section: 001
Instructor: L.Curl
Assignment: Program 6
Date: 04/18/92
PROBLEM STATEMENT:
The WildWood High School Student Data System is to be modified to make
use of variable arrays to minimize file reading. The student name
will be included using strings of CHAR variables. The program will be
designed to read the file completely (or as far as array space will permit)
and afterwards will make all accesses to student data within the arrays only.
INPUT/OUTPUT:
Input Data -
{Input is partially from a datafile. The file consists of an
undetermined number of lines of data in the format:
STUDENT NAME STUDENT ID HEIGHT(INCHES) WEIGHT(LBS) GPA
Menu selection input is obtained from the user at the keyboard in
the form of letters A thru E.}
Output Requirements-
{The output is to be in several forms:
A Main Menu of user selected option will be displayed.
The user may then select from the following forms of output:
A. A list of all student data in the arrays.
B. A list of students with GPA at or above the array's average GPA.
C. Display of the data for a particular student ID Number.
D. A list of all students at or above a user selected height. }
The students' height will be displayed in inches and in modular
feet/inch combinations. Furthermore, to include the use of
international metric units, the students' height and weight will
also be displayed in centimeters and kilograms (respectively).
All conversions will be done using integer arithmetic.
VARIABLES AND DATA STRUCTURES:
TYPE {Contained in TYPESMOD}
String = varying [25] of char;
IntArray = array [0..99] of integer;
RealArray = array [0..99] of real;
StrArray = array [0..99] of string;
VAR
Afile : Text; {Each line of this file contains student data}
NameArray : StrArray; {Array of Student Names}
IDArray : Intarray; {Array of Student ID Numbers}
WtArray : IntArray; {Array of Students' Weight(lbs)}
HtArray : IntArray; {Array of Students' Height (in)}
GPAArray : RealArray; {Array of Students' GPA}
Number : Integer; {Number of Students in Array}
AGPA : Real; {Average GPA of all students in the Array}
Listed : Integer; {Number of students listed}
IDEntry : Integer; {ID Number to Search for (Option C)}
HtEntry : Integer; {Minimum height(in.) to search for (Option D)}
Selection : Char; {Menu option selected by user}
I : Integer; {Used as a loop counter}
DISPLAY : Boolean; {Decision making: Whether a student is on the list}
ALGORITHM DESCRIPTION:
The main program calls a procedure that fills 5 arrays with data from
the datafile (procedure FillArrays). The main program then uses a
Repeat/Until loop to repeatedly display a main menu (procedure Showmenu),
gets the user's main menu selection (procedure GetChoice).
If the menu choice is a value other than 'E', a data search routine
is performed. Information required for the search is requested for
options 'C' and 'D'. A heading is printed, and a FOR loop is set up
to go thru all of the students in the array. If the student meets
the conditions desired by the search that the user has selected,
a procedure called LISTSTU is performed. This will display the
data for the student specified by the FOR loop. In the case that
option 'C' has been selected, the array location that the displayed
student data is stored at will be displayed (The index of the FOR loop).
If the menu choice 'E' has been selected, the program will exit
the WHILE loop and display exit messages and the program will end.
The CASE statement inside the FOR loop will set the boolean variable
DISPLAY to TRUE or FALSE, which will decide whether the student
will be displayed on the list being performed. This type of boolean
logic system will permit simple future expansions by allowing
the easy addition of another CASE alternative and the installation
of any type of boolean logic expression to form a student data list.
The student data will be stored in the arrays in the following format:
Array[0] {the first student record in the file}
Array[1] {the second student record in the file}
..... ..........
Array[Number-1] {the last student record in the file}
Array[Number] {undefined. The next record available for future storage}
Where Array is any one of the student data arrays listed above.
The variable Number represents the number of items stored in the arrays.
It also represents the subscript of the next available array element.
{----------------------------------------------------------------------------}
Procedure FillArrays(var Afile: text; {Data file to read from}
NameArray: StrArray; {Array of Student Names}
IDArray: Intarray; {Array of Student ID Numbers}
HtArray: IntArray; {Array of Students' Height(in)}
WtArray: IntArray; {Array of Students' Weight(lbs)}
GPAArray: RealArray; {Array of Students' GPA}
Number: Integer); {Number of Students in Array}
{Fills Arrays with student data from a data file}
PURPOSE- To read data from a data file and store it in separate arrays.
PARAMETERS/VARIABLES-
Variable Parameters-
var Afile: text; {Data file to read from}
var NameArray: StrArray; {Array of Student Names}
var IDArray: Intarray; {Array of Student ID Numbers}
var HtArray: IntArray; {Array of Students' Height(in)}
var WtArray: IntArray; {Array of Students' Weight(lbs)}
var GPAArray: RealArray; {Array of Students' GPA}
var Number: Integer); {Number of Students in Array}
Value Parameters- NONE
Local Variables- NONE
OUTPUT- The arrays will be modified to contain the contents of Afile,
and Number will represent the number of students stored in
the Arrays.
ALGORITHM- 1. Reset Afile and Set Number to 0.
2. Establish a While loop to go thru all the students in
Afile. Also avoid exceeding the Array limits.
3. Store the data for each student into the corresponding
arrays at the element [Number].
4. Increment Number, proceed to next loop.
{----------------------------------------------------------------------------}
Function AvgGPA(GPAArray : RealArray;
Number : Integer ) : Real; {Calculate the average GPA}
PURPOSE- Return the average GPA of all students in the Array
PARAMETERS/VARIABLES-
Variable parameters-NONE
Value parameters- GPAArray : RealArray; {GPA's of students in Array}
Number : Integer; {Number of students in Array}
Local Variables- I : Integer; {Used as a loop counter}
Total : Real; {Used to total all of the GPA's}
OUTPUT- Return the average GPA of all students in the Arrays.
ALGORITHM- 1. Set Total to 0.
2. Setup a FOR loop to go thru all the students in the Array.
3. Increment Total by the GPA for each student in the loop.
4. After the loop, assign Total/Number to the AvgGPA function.
{----------------------------------------------------------------------------}
Procedure Showmenu; {Displays a menu}
PURPOSE- To display a menu of selections
PARAMETERS/VARIABLES-
Variable parameters- NONE
Value parameters- NONE
Local Variables- NONE
OUTPUT- A display of a menu of available selections
ALGORITHM- 1. Display a main menu title
2. Display Options A thru E
{----------------------------------------------------------------------------}
Procedure GetChoice(var Selection:char); {Get selection from user}
PURPOSE- To prompt the user for a menu selection and accept input.
PARAMETERS/VARIABLES-
Variable parameters- Selection:char {Character entered by user}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a Prompt
ALGORITHM- 1.Displays a prompt for user input
2.Get user input, stored in Selection.
3.Translate any lower case letters to upper case.
4.Display an error message if an invalid option is selected.
{----------------------------------------------------------------------------}
Procedure GetID(IDEntry : Integer);
{ Gets user input for which ID number to search for when option C is selected }
PURPOSE- Get an ID number from user input. This procedure is intended
to be called when option C is selected from the main menu.
PARAMETERS/VARIABLES-
Variable parameters- IDEntry : Integer {ID number entered by user}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a prompt for the input of an ID number.
ALGORITHM-
1. Display a prompt for the input of an ID number to search for.
2. Accept user input for IDEntry.
{----------------------------------------------------------------------------}
Procedure GetHt(HtEntry : Integer);
{ Gets user input for what minimum height to use as a search parameter }
PURPOSE- Gets user input for a height (in inches) which is to be used
as a search parameter when listing all students that are
at least the height entered (Menu Option D).
PARAMETERS/VARIABLES-
Variable parameters- HtEntry : Integer {Height entered by user}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a prompt for the input of a height(in inches).
ALGORITHM-
1. Display a prompt for the input of a minimum height to search for.
2. Accept user input for HtEntry.
{----------------------------------------------------------------------------}
Procedure Heading; {Displays a Heading at the top of the output listing}
PURPOSE- To display a heading at the top of a list of output.
PARAMETERS/VARIABLES-
Variable parameters- NONE
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a heading at the top of a list of students.
The labels for each column of data will be displayed.
ALGORITHM-
1. Display a heading in the form:
List ID Height Weight Student Above
No. Student Name Number in ft+in cm lbs kg GPA ** Avg
---- ------------------------- ------ -- ----- -- --- --- ----- ------
{----------------------------------------------------------------------------}
Procedure ListStu(NameArray: StrArray; {Array of Student Names}
IDArray : Intarray; {Array of Student ID Numbers}
HtArray : IntArray; {Array of Students' Weight(lbs)}
WtArray : IntArray; {Array of Students' Height (in)}
GPAArray : RealArray; {Array of Students' GPA}
AGPA : Real; {The average of all GPA's in the array}
I : Integer; {Array location of student to list}
var Listed : Integer); {Counter of students in list}
{ Displays all of the data stored in the arrays for a given array location }
PURPOSE- To display the data for a single student stored at the
array location provided.
PARAMETERS/VARIABLES-
Variable parameters- Listed : Integer {Counter of students in list}
Value parameters-
NameArray: StrArray; {Array of Student Names}
IDArray : Intarray; {Array of Student ID Numbers}
HtArray : IntArray; {Array of Students' Weight(lbs)}
WtArray : IntArray; {Array of Students' Height (in)}
GPAArray : RealArray; {Array of Students' GPA}
AGPA : Real; {The average of all GPA's in the array}
I : Integer; {Subscript of student to be listed}
Local Variables-None
OUTPUT- A single student will be displayed and the corresponding values
in each array will be aligned in the appropriate columns.
ALGORITHM-
1. Increment by 1 the variable called Listed.
2. Display the following data in column form:
Listed
Student Name { All numerical/dimensional conversions }
Student ID { will take place in the form of }
Height in inches { mathematical expressions within }
Height in feet and inches { a single WRITE command. }
Height in centimeters
Weight in pounds
Weight in kilograms
Student GPA
3. Display ** , if the student has an above average GPA.
{----------------------------------------------------------------------------}
TESTING:
A single procedure will be tested using a driver to call it.
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course: 266-256 Introduction to Computer Science I
Section: 001
Instructor: Linda Curl
Assignment: PROGRAM 8
Date: 05/06/92
PROBLEM STATEMENT:
{ The purpose of the program is to demonstrate the use of binary
searching and sorting of arrays of data. }
INPUT/OUTPUT:
Input Data-
Input data used to generate the record arrays will be from a data file.
Data in the data file will be expected to be in the following format:
ITEM NUMBER ITEM NAME (12 characters) PRICE INVENTORY QUANTITY
Input data used to make a main menu selection and data used to
provide specific output will be from the keyboard (in general use).
Output Requirements-
Output data will consist of several parts:
1. A main menu will be displayed, requesting a main menu selection.
2. Menu Option 1 - Displays a complete Grocery Data List
3. Menu Option 4 - Displays Grocery Data of an individual item.
4. Messages will be displayed in the event of a program error
and when exiting the program (menu option 5).
VARIABLES AND DATA STRUCTURES:
TYPE { * Contained in TYPESMOD * }
String = varying [14] of char; {Define string variables}
ItemRec = record
IDNum : integer; {ID number of Item}
Name : string; {Name of Item}
RPrice : real; {Price of Item}
Quan : integer; {Quantity in inventory}
end; {ItemRec}
RecArray = array [1..150] of ItemRec; {Item Record Array}
VAR
Afile : Text; {Data file used to generate inventory record array}
Inventory : RecArray; {Array of inventory records}
Count : Integer; {Number of items currently in the record array}
Selection : Char; {Main menu selection to be entered by the user}
ALGORITHM DESCRIPTION:
The main program calls a procedure that fills 1 array with data from
the datafile,(FillRecArray). The main program then uses a Repeat/Until
loop to repeatedly display a menu of options (procedure Showmenu), get
the user's choice of options (procedure GetChoice), and activate the
selected procedure or function until the user selects option Exit.
The main program then leaves the loop, prints exit messages, and stops.
Options 2 and 3 will be installed to allow a sort of the inventory
data by either Item Price or Item Name, respectively.
{-----------------------------------------------------------------------------}
Procedure FillRecArray(var Afile : text; {Inventory Data File}
var Inventory : RecArray; {Inventory Record Array}
var Count : Integer); {Last Array Subscript Used}
PURPOSE- To fill the record array with inventory data from an inventory
data file.
PARAMETERS/VARIABLES-
Variable parameters- var Afile : text {Inventory Data File}
var Inventory : RecArray {Inventory Record Array}
var Count : Integer {Last Array Subscript Used}
Value parameters- NONE
Local Variables- NONE
OUTPUT-
Provides inventory data to the main program.
The data for each item will be stored in the records of the
array using subscripts 1 thru Count. The integer Count will
represent the number of used subscripts in the record array.
ALGORITHM-
1. Reset Count to 0, and reset the data file.
2. Uses WHILE to loop thru all of the inventory data in the data file.
Also it will avoid exceeding the size of the record array.
3. Increment Count by 1.
4. Store Inventory data in the record array at subscript [Count].
{-----------------------------------------------------------------------------}
Procedure ShowMenu;
PURPOSE- Displays a menu
PARAMETERS/VARIABLES-
Variable parameters- NONE
Value parameters- NONE
Local Variables- NONE
OUTPUT- Display the main menu of available options.
ALGORITHM- 1. Display rows of characters.
{-----------------------------------------------------------------------------}
Procedure GetChoice(var selection:char);
PURPOSE-
To get an input (from the user) for use as a main menu selection.
PARAMETERS/VARIABLES-
Variable parameters- var selection : char {Main menu selection}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a prompt for user input of a main menu selection.
ALGORITHM-
1. Display prompt for user input
2. Accept input.
{-----------------------------------------------------------------------------}
Procedure ListData(Inventory: RecArray; { Inventory Records Array }
Count: Integer); { Number of Records used in Array}
PURPOSE-
To display a complete listing of all inventory stored
in the inventory record array.
PARAMETERS/VARIABLES-
Variable parameters- None
Value parameters- Inventory : RecArray { Inventory Records Array }
Count: Integer { Number of Records used in Array}
Local Variables- I : Integer {Loop Counter}
OUTPUT- Displays a list of all inventory records stored in the array.
ALGORITHM-
1. Display list heading.
2. Loop thru all used subscripts of the record array.
3. Display all data for that item, including quantity.
{-----------------------------------------------------------------------------}
Procedure FindItem(Inventory: RecArray; { Inventory Records Array }
Count: Integer; { Number of Records used in Array }
ItemNumber: Integer; { Item number to be found }
var Location: Integer ); { Array location of item found }
PURPOSE-
To find the subscript of the inventory records array that
corresponds to a given item number using a binary search.
PARAMETERS/VARIABLES-
Variable parameters- var Location: Integer {Array location of item}
Value parameters- Inventory: RecArray; { Inventory Records Array }
Count: Integer; { Number of Records used in Array }
ItemNumber: Integer; { Item number to be found }
Local Variables- First : Integer; {Lower bound used in search}
Last : Integer; {Upper bound used in search}
I : Integer; { Approximate middle of search
interval used as a 'guess' }
OUTPUT- Provides the subscript of the inventory array that will
correspond to the ItemName parameter.
If the item name is not found in the record array,
a 0 will be returned by the procedure to indicate a failure
to locate that item name.
ALGORITHM-
1. Set First and Last to extreme subscript values.
2. Set Location to a 0, to indicate that the item has not
been found so far.
3. Setup a loop to continue search until item has been found,
or the search has failed to find the item within the
refined subinterval.
4. Set I to a value approximately in the middle of the
refined subinterval specified by First and Last.
5. Compare the ItemName to be found with the Name of the item
at subscript I in the Inventory.
6. If the ItemName is less, Refine the Upper bound.
7. If the ItemName is greater, Refine the Lower bound.
8. If Equal, set Location to I.
{-----------------------------------------------------------------------------}
Procedure Item(Inventory: RecArray; { Inventory Records Array }
Count: Integer ); { Number of Records used in Array }
PURPOSE-
To display all of the data in the inventory records array for
a given item number.
PARAMETERS/VARIABLES-
Variable parameters- NONE
Value parameters- Inventory: RecArray; { Inventory Records Array }
Count: Integer; { Number of Records used in Array }
Local Variables- ItemName : Integer {Item Name to find}
Location : Integer {Array subscript of item}
OUTPUT- Displays all data for a given item name.
An error message will be printed if the item is not found.
ALGORITHM-
1. Prompt for user input of an item name to be displayed.
2. Accept input
3. Call FindItem to get the Location in the array.
4. If the Location = 0 then display an error message.
5. If Location is not 0 then display a heading and display
all of the data for the item at the subscript Location.
{-----------------------------------------------------------------------------}
Procedure SortPrice(var Inventory: RecArray; { Inventory Records Array }
Count: Integer ); { Number of Records used in Array }
PURPOSE-
Use a bubble sort to rearrange the order of the records in the
Inventory Array such that the items will be in decending order by price.
PARAMETERS/VARIABLES-
Variable parameters- Inventory: RecArray { Inventory Records Array }
Value parameters- Count: Integer; { Number of Records used in Array }
Local Variables- I : Integer {Loop Counter}
Sorted : Boolean {Whether the sort is complete}
Temp : ItemRec {Temporary record, used for sorting}
OUTPUT- Rearranges the order of the records in the array such that
the item prices will be in decending order.
ALGORITHM-
1. Setup a repeat loop
2. Set Sorted to TRUE (Assume sort is complete)
3. Setup a FOR loop thru the subscripts (except the last).
4. Check for any discrepancy of the expected order of
two consecutive records (decending Prices).
5. If necessary, correct the order of the records by switching
the two records using a temporary record variable. Also
set the boolean variable Sorted to FALSE. ( The assumption
that the sort is complete has been contradicted )
6. Continue the REPEAT loop until the Sorted variable
remains true after the FOR loop has completed.
(Since every two consecutive records are sorted, by
transitivity of inequality, the whole array is sorted.)
{-----------------------------------------------------------------------------}
Procedure SortName(var Inventory: RecArray; { Inventory Records Array }
Count: Integer ); { Number of Records used in Array }
PURPOSE-
Use a bubble sort to rearrange the order of the records in the
Inventory Array such that the items will be in ascending order by name.
PARAMETERS/VARIABLES-
Variable parameters- Inventory: RecArray { Inventory Records Array }
Value parameters- Count: Integer; { Number of Records used in Array }
Local Variables- I : Integer {Loop Counter}
Sorted : Boolean {Whether the sort is complete}
Temp : ItemRec {Temporary record, used for sorting}
OUTPUT- Rearranges the order of the records in the array such that
the item names will be in ascending order.
ALGORITHM-
1. Setup a repeat loop
2. Set Sorted to TRUE (Assume sort is complete)
3. Setup a FOR loop thru the subscripts (except the last).
4. Check for any discrepancy of the expected order of
two consecutive records (ascending Names).
5. If necessary, correct the order of the records by switching
the two records using a temporary record variable. Also
set the boolean variable Sorted to FALSE. ( The assumption
that the sort is complete has been contradicted )
6. Continue the REPEAT loop until the Sorted variable
remains true after the FOR loop has completed.
(Since every two consecutive records are sorted, by
transitivity of inequality, the whole array is sorted.)
{-----------------------------------------------------------------------------}
Procedure AskExit(var Selection : Char); { Main Menu Selection Entered }
PURPOSE-
To verify that the user wants to exit the program.
PARAMETERS/VARIABLES-
Variable parameters- Selection : Char {Main Menu Selection Entered}
Value parameters- NONE
Local Variables- Entry : Char {User input Y or N}
OUTPUT- Displays a prompt asking whether the user wants to exit
ALGORITHM-
1. Display a prompt asking for the entry of a 'Y' or 'N'.
2. If anything other than 'y' or 'Y' is entered, the
Selection will be altered so that the menu loop will not end.
TESTING:
A driver will be used to test the binary search procedure.
STRUCTURE CHART:
========================================
BASIC INFORMATION:
Course: 256-266
Section: 001
Instructor: L.Curl
Assignment: Program 9
Date: 05/22/92
PROBLEM STATEMENT:
{The purpose of this program is to demonstrate the use of the
file of records data structure and the algorithms that create
such a file, sort it, and merge two sorted files of records.
A hex dump of the file of records is included.}
INPUT/OUTPUT:
Input Data- All input will be from a main inventory file of records and
a text file containing new inventory data.
Output Requirements- Output will be the creation of two files of records,
one containing new records, and the other containing
a merge of the old and new record files.
VARIABLES AND DATA STRUCTURES:
TYPE
String = varying [14] of char; {Define string variables}
ItemRec = record {Define Item Records}
IDNum : integer; {ID number of Item}
Name : string; {Name of Item}
RPrice : real; {Price of Item}
Quan : integer; {Quantity in inventory}
end; {ItemRec}
RecArray = array [1..150] of ItemRec; {Define Item Record Array}
RecFile = File of ItemRec; {Binary file of records}
VAR
Afile : RecFile; {INPUT-Original File of Inventory Records}
Bfile : Text; {INPUT-Text file of Inventory Update}
Cfile : RecFile; {OUTPUT-File of Records to Merge}
Dfile : RecFile; {OUTPUT-Merged file of Inventory Records}
Inventory : RecArray; {Array of Inventory Records to Merge}
Count : Integer; {Number of items in Record Array}
ALGORITHM DESCRIPTION:
The program reads a text file into an array and sorts that file on the
numeric field IDNUM. The program then creates a file of records
from that sorted array. It merges the newly created file with another
sorted file of the same type of records without losing the sorted order.
{-----------------------------------------------------------------------------}
Procedure FillRecArray(var Bfile : text; {Inventory Data File}
var Inventory : RecArray; {Inventory Record Array}
var Count : Integer); {Last Array Subscript Used}
PURPOSE- To fill the record array with inventory data from an inventory
data file.
PARAMETERS/VARIABLES-
Variable parameters- var Bfile : text {Inventory Data File}
var Inventory : RecArray {Inventory Record Array}
var Count : Integer {Last Array Subscript Used}
Value parameters- NONE
Local Variables- NONE
OUTPUT-
Provides inventory data to the main program.
The data for each item will be stored in the records of the
array using subscripts 1 thru Count. The integer Count will
represent the number of used subscripts in the record array.
ALGORITHM-
1. Reset Count to 0, and reset the data file.
2. Uses WHILE to loop thru all of the inventory data in the data file.
Also it will avoid exceeding the size of the record array.
3. Increment Count by 1.
4. Store Inventory data in the record array at subscript [Count].
{-----------------------------------------------------------------------------}
Procedure SortAscending(var Inventory: RecArray; { Inventory Records Array }
Count: Integer ); { Number of Records used }
PURPOSE-
Use a bubble sort to rearrange the order of the records in the
Inventory Array such that the items will be ascending by number.
PARAMETERS/VARIABLES-
Variable parameters- Inventory: RecArray { Inventory Records Array }
Value parameters- Count: Integer; { Number of Records used in Array }
Local Variables- I : Integer; {Loop counter}
NextBound : Integer; {Used to get UpperBound}
UpperBound : Integer; {Remaining Sort Interval}
Temp : ItemRec; {Temporary record storage}
OUTPUT- Rearranges the order of the records in the array such that
the item numbers will be in ascending order.
ALGORITHM-
1 Reset NextBound to Count
1. Setup a repeat loop
2. Set UpperBound to NextBound (Assume sort is complete)
3. Setup a FOR loop thru the subinterval (except the last).
4. Check for any discrepancy of the expected order of
two consecutive records (ascending ID Numbers).
5. If necessary, correct the order of the records by switching
the two records using a temporary record variable. Also
set NextBound to the loop index ( The assumption
that the sort is complete has been contradicted )
6. Continue the REPEAT loop until the NextBound variable
remains same as UpperBound after the FOR loop has completed.
(Since every two consecutive records are sorted, by
transitivity of inequality, the whole array is sorted.)
{-----------------------------------------------------------------------------}
Procedure WriteRecFile(var Cfile : RecFile ; {OUTPUT-File to be stored}
Inventory : RecArray ; {Array used to generated file}
Count : Integer ); {Number of array elements used}
PURPOSE-
This procedure stores the records in the record array as
a binary file of records.
PARAMETERS/VARIABLES-
Variable parameters- Cfile : RecFile; {OUTPUT-FIle to be stored}
Value parameters- Inventory : RecArray; {Array used to generate file}
Count : Integer; {Number of array elements used}
Local Variables- I : Integer; {Loop counter}
OUTPUT- A binary file of inventory records.
ALGORITHM-
1 Rewrite Cfile to accept records.
2. Setup a FOR loop thru all used array elements.
3. Write the records to Cfile.
{-----------------------------------------------------------------------------}
Procedure Merge(var Afile : RecFile; {Main Inventory Record File}
var Cfile : RecFile; {New Records to add/update}
var Dfile : RecFile ); {Merged Record File Output}
PURPOSE-
This procedure reads records from the old and new inventory
record files. The records are then merged into a new
sorted record file with the contents of both the original
file and the records to be added/updated. In the event of
a copy of an item in both files, the update record will
be put in the output.
PARAMETERS/VARIABLES-
Variable parameters-
Afile : RecFile {Main Inventory Record File}
Cfile : RecFile {New Records to add/update}
Dfile : RecFile {Merged Record File Output}
Value parameters- NONE
Local Variables- Situation : Integer; {Status of records}
OUTPUT- A binary file of inventory records consisting of the old
inventory file, with the additions and updates coming from
the new inventory records.
ALGORITHM-
1 Reset Afile and Cfile for reading.
2. Rewrite Dfile as output.
3. Setup a REPEAT loop
4. Decide on a Situation where:
0 means both files have ended.
1 means Afile has not ended, but Cfile has.
2 means Cfile has not ended, but Afile has.
3 means neither has ended, both item numbers are equal.
4 means neither has ended, Afile has smaller item number.
5 means neither has ended, Cfile has smaller item number.
5. In Situations 1 and 4, put Afile's record in output buffer.
6. In Situations 2,3 and 5, put Cfile's record in the buffer.
7. In Situations not zero, store the output buffer in the file.
8. In Situations 1,3 and 4, advance Afile's pointer.
9. In Situations 2,3 and 5, advance Cfile's pointer.
10. Continue the loop UNTIL Situation is zero.
{-----------------------------------------------------------------------------}
Procedure Listdata(var Dfile : RecFile);
PURPOSE-
To display records from a binary file of inventory records.
PARAMETERS/VARIABLES-
Variable parameters- Dfile : RecFile; {OUTPUT-File to be stored}
Value parameters- NONE
Local Variables- NONE
OUTPUT- Displays a binary file of inventory records.
ALGORITHM-
1 Reset Dfile for reading.
2. Display a heading.
3. Use a WHILE loop to go thru the file.
4. Display item record data in the file buffer.
5. Advance the pointer to the next record (If any).
{-----------------------------------------------------------------------------}
TESTING:
Create a driver to test the procedure to create a file of
records from a text file.
STRUCTURE CHART:
