0% found this document useful (0 votes)
175 views

13 Modularization

The document discusses modularization and modular programming. It defines modularization as dividing a problem into separate subtasks, each with a single purpose. Top-down design is introduced as a methodology to identify major tasks and further subtasks. Modules are described as self-contained sections of an algorithm dedicated to a single function. Hierarchy charts and intermodule communication using local/global variables and parameter passing are also covered. An example shows a modularized solution to a problem that prompts the user for 3 characters, sorts them, and outputs them until "XXX" is entered.

Uploaded by

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

13 Modularization

The document discusses modularization and modular programming. It defines modularization as dividing a problem into separate subtasks, each with a single purpose. Top-down design is introduced as a methodology to identify major tasks and further subtasks. Modules are described as self-contained sections of an algorithm dedicated to a single function. Hierarchy charts and intermodule communication using local/global variables and parameter passing are also covered. An example shows a modularized solution to a problem that prompts the user for 3 characters, sorts them, and outputs them until "XXX" is entered.

Uploaded by

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

Modularization

Department of Electrical Engineering and Information


Technology
Universitas Gadjah Mada

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

look at each of the subtasks and identify within them further


subtasks, and so on
this process of identifying first the major tasks, then further
subtasks within them, is known as top-down design or functional
decomposition.
each of the subtasks or functions will become a module within a
solution algorithm or program

Modularization
A module

section of an algorithm
dedicated to a single function
makes the algorithm simpler
more systematic
free of errors

Modularization is the process of dividing a problem


into separate tasks, each with a single purpose.
Top-down design methodology allows the
programmer to concentrate on the overall design of
the algorithm without getting too involved with the
details of the lower-level modules.

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.

Process three characters


Design a solution algorithm that will
prompt a terminal operator for three
characters, accept those characters as
input, sort them into ascending sequence
and output them to the screen. The
algorithm is to continue to read
characters until XXX is entered.

Defining diagram
Input

Processing

Output

char_1
char_2
char_3

Prompt for characters


Accept three characters
Sort three characters
Output three characters

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.

may also be referred to


a structure chart
a visual table of contents.

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

Process three characters


Controlling
module
or
Calling module
or
Mainline

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

Process three characters


Design a solution algorithm that will
prompt a terminal operator for three
characters, accept those characters as
input, sort them into ascending sequence
and output them to the screen. The
algorithm is to continue to read
characters until XXX is entered.

Defining diagram
Input

Processing

Output

char_1
char_2
char_3

Prompt for characters


Accept three characters
Sort three characters
Output three characters

char_1
char_2
char_3

Process three characters

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.

Local (defined within a subordinate module)


not known to the calling module, or to any other module
the scope is simply the module in which it is defined
the lifetime is limited to the execution of the single
subroutine in which it is defined
can reduce what is known as program side effects

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)

The names that the respective modules give to


their parameters need not be the same but their
number, type and order must be identical.

Parameters
Formal parameters

appear when a submodule is defined

Actual parameters

variables and expressions that are passed to a


submodule

For example

a mainline may call a module with an actual


parameter list

the module may have been declared with the


following formal parameter list:

Print_page_headings (page_count, line_count)

Print_page_headings (page_counter, line_counter)

Although the parameter names are different, the


actual and formal parameters will correspond.

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

Increment two counters


Design an algorithm that will increment two counters from
to 10 and then output those counters to the screen. Your
program is to use a module to increment the counters.

Defining diagram
Input

Processing

Output

counter1
counter2

Increment counters
Output counters

counter1
counter2

Solution algorithm

Increm ent_tw o_counters


Set counter1, counter2 to zero
D O I= 1 to 10
Increm ent_counter (counter1)
Increm ent_counter (counter2)
O utput to the screen counter1,counter2
EN D D O
EN D

Increm ent_counter (counter)


counter = counter + 1
EN D

Hierarchy charts and


parameters
Parameters can be incorporated into a hierarchy
chart using the following symbols:

Data parameters contain the actual variables or


data items that will be passed between modules.
Status parameters act as program flags and
should contain just one of two values: true or
false.

Process three characters


Design a solution algorithm that will prompt a terminal operator
for three characters, accept those characters as input, sort them
into ascending sequence and output them to the screen. The
algorithm is to continue to read characters until XXX is entered.

Defining diagram
Input

Processing

Output

char_1
char_2
char_3

Prompt for characters


Accept three characters
Sort three characters
Output three characters

char_1
char_2
char_3

Group the activities into


modules
Construct a hierarchy chart

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

Print_three_characters (char_1, char_2, char_3)


O utput to the screen char_1, char_2, char_3
EN D

Group the activities into


modules
Construct a hierarchy chart

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

Sw ap_tw o_characters (fi


rst_char, second_char)
tem p = fi
rst_char
fi
rst_char = second_char
second_char = tem p
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.

Calculate employees pay


A program is required by a company to read an employees
number, pay rate and the number of hours worked in a week. The
program is then to validate the pay rate and the hours worked
fields and, if valid, compute the employees weekly pay and print
it along with the input data.
Validation: According to the companys rules, the maximum hours
an employee can work per week is 60 hours, and the maximum
hourly rate is $25.00 per hour. If the hours worked field or the
hourly rate field is out of range, the input data and an appropriate
message is to be printed and the employees weekly pay is not to
be calculated.
Weekly pay calculation: Weekly pay is calculated as hours worked
times pay rate. If more than 35 hours are worked, payment for
the overtime hours worked is calculated at time-and-a-half.

Define the problem


Input

Processing

Output

emp_no
pay_rate
hrs_worked

Read employee details


Validate input fields
Calculate employee pay
Print employee details

emp_no
pay_rate
hrs_worked
emp_weekly_pay
error_message

Group the activities into modules


Construct a hierarchy chart

tablish the logic of the mainline of the algorithm,


ng pseudocode
Compute_employee_pay
Read_employee_details (employee_details)
DOWHILE more records
Validate_input_fields (employee_details, valid_input_fields)
IF valid_input_fields THEN
Calculate_employee_pay (employee_details)
Print_employee_details (employee_details)
ENDIF
5
Read_employee_details (employee_details)
ENDDO
END
1
2
3
4

velop the pseudocode for each successive module


the hierarchy chart
Read_employee_details (employee_details)
6
Read emp_no, pay_rate, hrs_worked
END

Validate_input_fields (employee_details, valid_input_fields)


7
Set valid_input_fields to true
8
Set error_message to blank
9
IF pay_rate > $25 THEN
error_message = Pay rate exceeds $25.00
Print emp_no, pay_rate, hrs_worked, error_message
valid_input_fields = false
ENDIF
10 IF hrs_worked > 60 THEN
error_message = Hours worked exceeds 60
Print emp_no, pay_rate, hrs_worked, error_message
valid_input_fields = false
ENDIF
END
Calculate_employee_pay (employee_details)
11 IF hrs_worked <= 35 THEN
emp_weekly_pay = pay_rate * hrs_worked
ELSE
overtime_hrs = hrs_worked - 35
overtime_pay = overtime_hrs * pay_rate * 1.5
emp_weekly_pay = (pay_rate * 35) + overtime_pay
ENDIF
END
Print_employee_details (employee_details)
12 Print emp_no, pay_rate, hrs_worked, emp_weekly_pay
END

Product orders report


The Acme Spare Parts Company wants to produce a product orders
report from its product orders file. Each record on the file contains
the product number of the item ordered, the product description,
the number of units ordered, the retail price per unit, the freight
charges per unit, and the packaging costs per unit.
Your algorithm is to read the product orders file, calculate the total
amount due for each product ordered and print these details on
the product orders report.
The amount due for each product is calculated as the product of
the number of units ordered and the retail price of the unit. A
discount of 10% is allowed on the amount due for all orders over
$100.00. The freight charges and packaging costs per unit must
be added to this resulting value to determine the total amount
due.

Product orders report


The output report is to contain headings and
column headings as specified in the following
chart:
ACME SPARE
PART

ORDERS REPORT

PAGE xx

PRODUCT
NO

PRODUCT
DESCRIPTION

xxxx

xxxxxxxxxx

UNITS
ORDERED
xxx

TOTAL AMOUNT
DUE
xxxxx

xxx product number,


xxxxx
Eachxxxxdetail linexxxxxxxxxx
is to contain the
product description, number of units ordered and
the total amount due for the order. There is to be an
allowance of 45 detail lines per page.

You might also like