13 Modularization
13 Modularization
Modularization
Objectives
To introduce modularization as a means of
dividing a problem into subtasks
To present hierarchy charts as a pictorial
representation of modular program structure
To discuss intermodule communication, local
and global variables, and the passing of
parameters between modules
To develop programming examples that use a
simple modularized structured
Modularization
Programming problems increase in complexity
becomes more difficult to consider the solution as a whole
dividing the problem into smaller parts
Steps
identify the major tasks to be performed in the problem
divide the problem into sections that represent those tasks
these sections can be considered subtasks or functions
Modularization
A module
section of an algorithm
dedicated to a single function
makes the algorithm simpler
more systematic
free of errors
Modularization
Modular name convention
Describe the work to be done as a single specific function
Use a verb, followed by a two-word object
Example:
Print_page_heading
Calculate_sales_tax
Validate_input_date
The mainline
show the main processing functions, and the order in which
they are to be performed
show the flow of data and the major control structures
easy to read, be of manageable length and show sound
logic structure
Modularization
Benefits of modular design
Ease of understanding
each module should perform just one function.
Reusable code
modules used in one algorithm can also be used in
others.
Elimination of redundancy
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 effect on other modules within the
algorithm.
Defining diagram
Input
Processing
Output
char_1
char_2
char_3
char_1
char_2
char_3
Solution algorithm
Process_three_characters
Prom pt the operator for char_1,char_2,char_3
G et char_1, char_2,char_3
D O W H ILE N O T (char_1 = XAN D char_2 = XAN D char_3 = X)
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
O utput to the screen char_1,char_2, char_3
Prom pt operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D D O
EN D
Solution algorithm
Process_three_characters
Prom pt the operator for char_1,char_2,char_3
G et char_1, char_2,char_3
D O W H ILE N O T (char_1 = XAN D char_2 = XAN D char_3 = X)
Sort_three_characters
O utput to the screen char_1,char_2,char_3
Prom pt operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D D O
EN D
Sort_three_characters
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
EN D
Hierarchy charts
Hierarchy charts
Representation of the functions or modules graphically
in a diagram.
It shows not only the names of all the modules but
also their hierarchical relationship to each other.
This diagrammatic form of hierarchical relationship
appears similar to an organizational chart of personnel
within a large company.
Solution algorithm
Process_three_characters
Prom pt the operator for char_1,char_2,char_3
G et char_1, char_2,char_3
D O W H ILE N O T (char_1 = XAN D char_2 = XAN D char_3 = X)
Sort_three_characters
O utput to the screen char_1,char_2,char_3
Prom pt operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D D O
EN D
Sort_three_characters
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
EN D
Subordinate
module
or
Called Module
Solution algorithm
Process_three_characters
Prom pt the operator for char_1,char_2,char_3
G et char_1, char_2,char_3
D O W H ILE N O T (char_1 = XAN D char_2 = XAN D char_3 = X)
Sort_three_characters
O utput to the screen char_1,char_2,char_3
Prom pt operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D D O
EN D
Subordinate
module
or
Called Module
Controlling
module
or
Calling module
or
Mainline
Sort_three_characters
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
EN D
Defining diagram
Input
Processing
Output
char_1
char_2
char_3
char_1
char_2
char_3
Hierarchy charts
Solution algorithm
Process_three_characters
Read_three_characters
D O W H ILE N O T (char_1 = X AN D char_2 = XAN D char_3 = X)
Sort_three_characters
Print_three_characters
Read_three_characters
EN D D O
EN D
Read_three_characters
Prom pt the operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D
Sort_three_characters
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
EN D
Print_three_characters
O utput to the screen char_1, char_2, char_3
EN D
Communication between
modules
We should consider not only the division of
the problem into modules but also the flow
of information between the modules.
The fewer and simpler the communications
between modules, the easier it is to
understand and maintain one module
without reference to other modules.
This flow of information, called intermodule
communication, can be accomplished by the
scope of the variable (local or global data) or
the passing of parameters.
Scope of a variable
The scope of a variable is the portion of a program in
which that variable has been defined and to which it
can be referred.
Global (defined within a mainline)
can be used by all the modules in a program
the scope is the whole program
the lifetime spans the execution of the whole program.
Side effects
A side effect is a form of crosscommunication of a module with other parts
of a program.
It occurs when a subordinate module alters
the value of a global variable inside a
module.
Side effects are not necessarily detrimental.
However, they do tend to decrease the
manageability of a program.
Parameters
Parameters are simply data items transferred
from a calling module to its subordinate module
at the time of calling.
When a calling module calls a subordinate
module in pseudocode, it must consist of the
name of the called module with a list of the
parameters to be passed to the called module
enclosed in parentheses, example:
Print_page_headings (page_count, line_count)
Parameters
Formal parameters
Actual parameters
For example
Parameters
Parameters may have one of three
functions:
To pass information from a calling module to
a subordinate module.
To pass information from a subordinate
module to its calling module.
To fulfill a two-way communication role.
Parameters
Value parameters
only pass data one way
the called module may not modify the value
of the parameter
when the submodule has finished processing,
the value of the parameter returns to its
original value
Reference parameters
Reference parameters can pass data to a
called module where that data may be
changed and then passed back to the calling
module, the reference address of the
Defining diagram
Input
Processing
Output
counter1
counter2
Increment counters
Output counters
counter1
counter2
Solution algorithm
Defining diagram
Input
Processing
Output
char_1
char_2
char_3
char_1
char_2
char_3
Solution algorithm
Process_three_characters
Read_three_characters
D O W H ILE N O T (char_1 = X AN D char_2 = XAN D char_3 = X)
Sort_three_characters
Print_three_characters
Read_three_characters
EN D D O
EN D
Read_three_characters
Prom pt the operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D
Sort_three_characters
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
EN D
Print_three_characters
O utput to the screen char_1, char_2, char_3
EN D
Solution algorithm
Process_three_characters
Read_three_characters (char_1, char_2, char_3)
D O W H ILE N O T (char_1 = X AN D char_2 = XAN D char_3 = X)
Sort_three_characters (char_1, char_2,char_3)
Print_three_characters (char_1,char_2,char_3)
Read_three_characters (char_1, char_2,char_3)
EN D D O
EN D
Read_three_characters (char_1, char_2, char_3)
Prom pt the operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D
Sort_three_characters (char_1,char_2,char_3)
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
IF char_2 > char_3 TH EN
tem p = char_2
char_2 = char_3
char_3 = tem p
EN D IF
IF char_1 > char_2 TH EN
tem p = char_1
char_1 = char_2
char_2 = tem p
EN D IF
EN D
Solution algorithm
Process_three_characters
Read_three_characters (char_1,char_2, char_3)
D O W H ILE N O T (char_1 = X AN D char_2 = XAN D char_3 = X)
Sort_three_characters (char_1,char_2,char_3)
Print_three_characters (char_1,char_2,char_3)
Read_three_characters (char_1, char_2,char_3)
EN D D O
EN D
Read_three_characters (char_1, char_2, char_3)
Prom pt the operator for char_1,char_2,char_3
G et char_1,char_2,char_3
EN D
Print_three_characters (char_1,char_2, char_3)
O utput to the screen char_1,char_2, char_3
EN D
Sort_three_characters (char_1,char_2,char_3)
IF char_1 > char_2 TH EN
Sw ap_tw o_characters (char_1,char_2)
EN D IF
IF char_2 > char_3 TH EN
Sw ap_tw o_characters (char_2,char_3)
EN D IF
IF char_1 > char_2 TH EN
Sw ap_tw o_characters (char_1,char_2)
EN D IF
EN D
Steps in modularization
1. Define the problem by dividing it into its three
components: input, output and processing.
2. Group the activities into subtasks or functions to
determine the modules that will make up the program.
3. Construct a hierarchy chart to illustrate the modules
and their relationship to each other.
4. Establish the logic of the mainline of the algorithm in
pseudocode.
5. Develop the pseudocode for each successive module
in the hierarchy chart.
6. Desk check the solution algorithm.
Processing
Output
emp_no
pay_rate
hrs_worked
emp_no
pay_rate
hrs_worked
emp_weekly_pay
error_message
ORDERS REPORT
PAGE xx
PRODUCT
NO
PRODUCT
DESCRIPTION
xxxx
xxxxxxxxxx
UNITS
ORDERED
xxx
TOTAL AMOUNT
DUE
xxxxx