0% found this document useful (0 votes)
168 views64 pages

Modularisation and General Algorithms For Common Business Problems

The document discusses modularization techniques for program design including defining modules, constructing hierarchy charts, and providing examples. It also covers array processing concepts such as initializing arrays, searching arrays, and writing array contents. The document provides guidance on applying modular design principles and array techniques to a sample business problem involving calculating gas bills.

Uploaded by

Muhammad Rafif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views64 pages

Modularisation and General Algorithms For Common Business Problems

The document discusses modularization techniques for program design including defining modules, constructing hierarchy charts, and providing examples. It also covers array processing concepts such as initializing arrays, searching arrays, and writing array contents. The document provides guidance on applying modular design principles and array techniques to a sample business problem involving calculating gas bills.

Uploaded by

Muhammad Rafif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 64

Course : Program Design Methods

Effective Period : July 2018

Modularisation and General


Algorithms for Common
Business Problems

Session 07
(Tutorial)
Acknowledgement

These slides have been adapted from


Simple Program Design : A Step by step approach.,
Lesley Anne Robertson, Fifth Edition,
Course Technology. ISBN: 978-1-4239-0132-7.
Chapter 7 - 8
Learning Objectives
LO 3 : Design the application using program
design method
LO 4 : Demonstrate the use of program design
method
Contents
• Modularisation
• Hierarchy Charts or structure charts
• Steps in modularisation
• Programming examples using modules
• Program Structure
• Report generation with page break
• Single – level control break
• Program Structure
• Report Generation with page break
• Single - Level control break
• Multiple level control break
• Sequential file update
Array Processing
Array Processing
• An array is a data structure that is made up of
a number of variables all of which have the
same data type.
• The individual data items that make up the
array are referred to as the elements of the
array. Elements in the array are distinguished
from one another by the use of an index or
subscript, enclosed in parentheses, following
the array name.
• Example :
scores (6)
scores (index)
Operations on Arrays
• The most typical operations performed on
arrays are
– Loading a set of initial values into the
elements of an array
– Processing the elements of an array
– Searching an array, using a linear or binary
search, for a particular element
– Writing the content of an array to a report
Example
• Example: each element of the array is
accumulated into a variable called sum. When
all elements have been added, the variable
sum is printed
Find_sum_of_elements
set sum to zero
DO index=1 to number_of_elements
sum = sum + array(index)
ENDDO
print sum
END
Initializing the Elements of an Array
Initialising
the elements of an array
• Because an array is an internal data structure,
initial values must be placed into the array
before any information can be retrieved from
it.
• These initial values can be assigned to the
elements of the array as constants, or they
can be read into the array from a file
Loading constant values
into an array
• This method should only be used when the data in
the array is unlikely to be changes
• Example
initialise_month_table
month_table(1) = ‘January ’
month_table(2) = ‘February ’
..
..
month_table(12) = ‘December ’
END
• Note that each element of the array must be the size
of the largest element – in this case September – so,
the shorter month names must be padded with
blanks (spaces)
Arrays of variable size
• In some programs, the number of entries in
an array can vary. The algorithm for loading
values into an array of variable size must
include a check to ensure that no attempt is
made to load more entries into the array than
there are elements
Paired arrays
• Many arrays in business application are
paired; that is, there are two arrays that have
the same number of elements.
• The arrays are paired because the elements in
the first array correspond to the elements in
the same position in the second array
• For example:
product_codes(index)=input product_code
selling_prices(index)=input selling_price
Searching an Array
Searching an array
• The reasons for searching an array may be :
– To edit and input value – that is , to check that it is
a valid element of an array
– To retrieve information from an array
– To retrieve information from a corresponding
element in a paired array
• There are two ways for searching an array
– Linear search
– Binary search
Array searching
techniques
• A linear search of an array
• Involves looking at each of the elements of the
array, one by one ,starting with the first element.
• A binary search of an array
• locates the middle element of array first, and
determines if the element being searched for is in
the first or second half of the table.
• The search then points to the middle element of the
relevant half table and the comparison is repeated.
• This technique of continually halving the number of
elements under consideration is continued until the
data item being searched for is found or its absence
is detected
Writing out the Contents of an Array
Writing out
the contents of an array
• Writing out the contents of an array involves starting
with the first element of the array and continuing
until all elements have been written. This can be
represented by a simple DO loop
• Example
write_values_of_array
DO index = 1 to number_of_elements
print array(index)
ENDDO
END
Two Dimensional Array
Two dimensional array
• An element of a two-dimensional array is specified
using the name of the array, followed by two
subscript, enclosed in parentheses and separated by
a comma
• Example
freight_charges(1,1)
freight_charges(row_index,column_index)
• A two-dimensional array is loaded in columns within
row order; all the columns for row one are loaded
before moving to row two and loading the column for
that row, and so on
Writing out the contents
of two dimensional array
• To write out the contents of a two-dimensional array, write
out the elements in the column within a row, before moving
on to the next row.
• Example :
write_value_of_array
set number_of_rows to required value
set number_of_columns to required value
DO row_index = 1 to number_of_columns
DO column_index=1 to number_of_columns
print array(row_index,column_index)
ENDDO
ENDDO
END
Modularisation
Modularisation
• Modularisation is the process of dividing a
problem into separate tasks, each with a
single purpose.
• Top down design methodology allows you to
concentrate on the overall design of the
algorithm before getting involved with the
details of the lower-level modules
Benefits of modular design

• Benefit of modular design


– Ease of understanding : each module should
perform just one function
– Reusable code : modules used in one program can
also be used in other programs
– Elimination of redundancy : using modules can help
to avoid the repetition of writing out the same
segment of code more than once
– Efficiency of maintenance : each module should be
self-contained and have little or no other modules
within the program
Hierarchy Chart or Structure Chart
Hierarchy chart
or structure chart
• Once the tasks have been grouped into functions or
modules, these modules can be represented
graphically in a diagram. This diagram is known as
hierarchy chart
• A hierarchy chart uses a tree-like diagram of boxes,
each box represents a module in the program and the
lines connecting the boxes represent the relationship
of the modules to others in the program hierarchy.
• At the top of the hierarchy chart is the controlling
module or mainline.
Hierarchy Chart
• Example
Steps in Modularisation
Steps in modularisation
• There are six steps in modularisation
1. Define the problem
2. Group the activities into modules
3. Construct a hierarchy chart
4. Establish the logic of the mainline of the
algorithm, using pseudocode
5. Develop the pseudocode for each successive
module in the hierarchy chart
6. Desk check the solution algorithm
Most common modules
• The three most common modules are
– An initial processing module, containing the steps
to be performed at the beginning of the algorithm
before the loop begins
– A processing module inside the loop containing all
the steps necessary to process one record or piece
of data
– A final processing module, containing the steps to
be performed at the end of the algorithm, outside
the loop
Programming Example
Example
• The Domestic Gas Supply Company records its customers’ gas
usage figures on a customer usage file. Each record on the file
contains the customer’s number, name, address, and gas usage
expressed in cubic metres. Design a solution algorithm that will
read the customer usage file, calculate the amount owing for gas
usage for each customer, and then print a report listing each
customer’s number, name, address, gas usage and the amount
owing
• The company bills its customers according to the following rate :
if the customer’s usage is 60 cubic metres or less, a rate of $2.00
per cubic metre is applied; if the customer’s usage is more than
60 cubic metres, than a rate of $1.75 per cubic metre is applied
for the first 60 cubic metres and $1.50 per cubic metre for the
remaining usage
• At the end of the report, print the total number of customers
and the total amount owing to the company
Example
A. Define the problem

Input Process Output


customer usage Print heading Heading line
records Read usage records Customer details
• customer_number Calculate amount owing • customer_number
• name Print customer details • name
• address Compute total customers • address
• gas_usage Compute total amount • gas_usage
owing • amount_owing
Print totals total_customers
total_amount_owing
B. Group the activities into modules
Four modules will be used in the solution algorithm:
• A module to perform some initial processing before loop
• A module to calculate the amount owing for each customer
• A module to print the customer details
• A module to print the totals after exiting the loop
Example
C. Construct a hierarchy chart
Example
D. Establish the logic of the mainline of the algorithm, using
pseudocode
Bill_gas_customers
1 Perform_initial_processing
2 read customer record
3 DOWHILE more records
4 calculate_amount_owing
5 print_customer_details
6 read customer record
ENDDO
7 print_gas_totals
END
Example
E. Develop the pseudocode for each successive module in the
hierarchy chart

Perform_initial_processing
8 print ‘CUSTOMER USAGE FIGURES’ heading
9 set total_Customer to zero
10 set total_amount_owing to zero
END

Calculate_amount_owing
11 IF usage <= 60 THEN
amount_owing = usage * $2.00
ELSE
amount_owing=(60*1.75)+((usage - 60)*$1.50)
ENDIF
12 add amount_owing to total_amount_owing
END
Example
E. Develop the pseudocode for each successive module
in the hierarchy chart
Print_customer_details
13 print customer_number, name, address,
gas_usage, amount_owing
14 add 1 to total_customers
END

Print_gas_totals
15 print total_customers
16 print total_amount_owing
END
Example
F. Desk Check the solution algorithm
• The desk checking of an algorithm with modules is
not different from the method developed for
previous examples
1. Create some valid input test data
2. List the output that the input data is expected to
produce
3. Use a desk check table to walk the data through
the mainline of the algorithm to ensure that the
expected output is achieved. When a sub
module is called, walk the data through each
line of that module as well and then return to
the calling module
Example
F. Desk Check the solution algorithm
1. Input data
three test cases will be used to test the algorithm

Customer record Gas_usage(cubic metres)


customer 1 40
customer2 61
customer3 80
EOF
Example
F. Desk Check the solution algorithm
2. Expected results

CUSTOMER USAGE FIGURES

Customer Details Gas Usage Amount Owing


customer1 name, address 40 $80.00
customer2 name, address 61 $106.50
customer3 name, address 80 $135.00
Total customers 3
Total amount owing $321.50
Example
F. Desk Checking the solution algorithm
3. Desk check table
statement customer DOWHILE amount_o total_amount_ total_
gas_usage Heading
number number condition wing owing customers

1,8 print
9,10 0 0
2 customer1 40
3 TRUE
4,11 $80.00
12 $80.00
5,13,14 print print print 1
6 customer2 61
3 TRUE
4,11 $106.50
12 $186.50
5,13,14 print print print 2
6 customer3 80
3 TRUE
4,11 $135.00
12 $321.50
5,13,14 print print print 3
6 EOF
3 FALSE
7,15,16 print print
Program Structure
Program Structure
• The general solution algorithm consisted of a mainline
module and three subordinate modules. These are :
– An initial processing module, containing the steps
to be performed at the beginning of the algorithm,
before the loop begins
– A processing module inside the loop containing all
steps necessary to process one record or piece of
data
– A final processing module, containing the steps to
be performed at the end of the algorithm, outside
the loop
Hierarchy Chart
• The hierarchy chart looked like this
Report Generation with Page Break
Report generation
with page break
• Most reports require page heading lines,
column heading lines, detail lines and total
lines. Reports are also required to skip to a
new page after pre-determined number of
detail lines have been printed
Example
• A typical report might look like this :

RAINBOW CLOTHING COMPANY

12/8/2008 CURRENT ACCOUNT BALANCES PAGE : 3

CUSTOMER ACCOUNT
NUMBER CUSTOMER NAME CUSTOMER ADDRESS BALANCE
12121 Fresh's Boutique Mall of Jakarta $300.00
12122 Cutest's Boutique Jakarta Shops Center $400.00
Total Customers on File 150
Total customers with balance owing 100
Total Balance owing $5,000.00
Report generation
with page break
• Hierarchy Chart
– The general solution algorithm for processing a
sequential file can be extended by the addition of three
new modules to cater for these report requirements
these new modules are Print_page_heading,
Print_detail_line, and Accumulate_total_fields
Single Level Control Break
Single level control break
• A control break total line is a summary line
for a group of records that contain the
same record key. this record key is referred
as the control field.
• This control field is used to identify a record
or a group of records within a file. A control
break occurs each time there is a change in
value of the control field. Thus, control
break total lines are printed each time a
control break is detected
Single level control break
• The sample of single level control break
GOLDEN COMPUTER COMPANY

12/8/2011 SALES REPORT BY SALES PERSON PAGE : 1

SALES PERSON PRODUCT EXTENSION


NUMBER SAELS PERSON NAME NUMBER QTY SOLD PRICE AMOUNT
A001 Angela 1051 2 $ 10.00 $ 20.00
1055 4 $ 20.00 $ 80.00
1080 4 $ 30.00 $ 120.00
Sales total for Angela $ 220.00

A004 Bryan 1051 1 $ 10.00 $ 10.00


1055 2 $ 20.00 $ 40.00
Sales total for Bryan $ 50.00
Report sales total $ 270.00
Single level control break
• Two things that need to be considered when
designing a control break program
– The file to be processed must have been sorted
into control field sequence. If the file has not
been sorted, erroneous results will be occur
– Each time a record is read from the input file, the
control field on the current record must be
compared with the control field on the previous
record. If the control fields are different, a
control break total line must be printed for the
previous set or records, before the current
records is processed
Single level control break
• Hierarchy Chart
Single level control break

All control break report program will require


the following variables:
• A variable named this_control_field, which will
hold the control field of the record just read
• A variable named prev_control_field, which will
hold the control field of the previous record
• One or more variables to accumulate the
control break totals
• One or more variables to accumulate the
report totals
Multiple Level Control Break
Multiple level control break
• The multiple level control break might look like this
GOLDEN COMPUTER COMPANY

12/8/2011 SALES REPORT BY SALES PERSON PAGE : 1

SALES PERSON PRODUCT EXTENSION


DEPT SAELS PERSON NAME QTY SOLD PRICE
NUMBER NUMBER AMOUNT
01 A001 Angela 1051 2 $ 10.00 $ 20.00
1055 4 $ 20.00 $ 80.00
1080 4 $ 30.00 $ 120.00
Sales total for Angela $ 220.00

A004 Bryan 1051 1 $ 10.00 $ 10.00


1055 2 $ 20.00 $ 40.00
Sales total for Bryan $ 50.00
Sales total for Dept 01 $ 270.00

;02 B002 Cassandra 1035 1 $ 15.00 $ 15.00


1055 2 $ 20.00 $ 40.00
Sales total for Cassandra $ 55.00
Sales total for Dept 02 $ 55.00
Report sales total $ 325.00
Multiple level control break
• The concept that applied to a single level
control break program also apply to multiple-
level-control-break program
– The input file must be sorted into control
field sequence. When there is more than
one control field, the file must be sorted
into a sequence of minor control field
within major control field
– Each time a record is read from the file, the
control field (minor and major) on the
current record must be compared with the
control field of the previous record.
Hierarchy chart
Sequential File Update
Sequential file update
• Sequential file update involve updating a master file by
applying update transaction to a transaction file. Both the
master file and the transaction are sequential. A new file
master file that incorporates the updated transaction is
produced. Usually, audit reports and error reports are also
printed
System concepts
• System concepts
– Master file : is a file that contains permanent and semi
permanent information about the data entities it contains
– Transaction file : contains all the data and activities that
are included on the master file . There are usually three
types of update transaction on this file.
• Add a new record
• Update or change an existing record
• Delete and existing record
– Audit report : is a detailed list of all transactions that were
applied to the master file
– Error report : is a detailed list of errors that occurred
during the processing of the update
References
• Simple Program Design : A Step by step approach.,
Lesley Anne Robertson, Fifth Edition,
Course Technology. ISBN: 978-1-4239-0132-7.
• Working with files,
http://www.thevbprogrammer.com/Ch06/06-01-
SeqFiles1.htm
• Functions, http://aelinik.free.fr/c/ch15.htm
References
• Simple Program Design : A Step by step approach.,
Lesley Anne Robertson, Fifth Edition,
Course Technology. ISBN: 978-1-4239-0132-7.
• General Arrays,
http://www.thevbprogrammer.com/Ch07/07-01-
Arrays1.htm
• Storing similar data items,
http://aelinik.free.fr/c/ch12.htm
• Scope and Storage, http://aelinik.free.fr/c/ch14.htm
• Working with files,
http://www.thevbprogrammer.com/Ch06/06-01-SeqFiles1.htm
• Functions, http://aelinik.free.fr/c/ch15.htm
Q&A

You might also like