13 modularization
TRANSCRIPT
-
7/25/2019 13 Modularization
1/39
ModularizationDepartment of Electrical Engineering and InformationTechnology
Universitas Gadjah Mada
-
7/25/2019 13 Modularization
2/39
Modularization
ObjectivesTo introduce modularization as a means of
dividing a problem into subtass
To present hierarchy charts as a pictorialrepresentation of modular program structure
To discuss intermodule communication! localand global variables! and the passing of
parameters bet"een modulesTo develop programming e#amples that use a
simple modularized structured
-
7/25/2019 13 Modularization
3/39
Modularization
$rogramming problems increase in comple#ity becomes more di%cult to consider the solution as a "hole
dividing the problem into smaller parts
&teps identify the major tass to be performed in the problem
divide the problem into sections that represent those tass
these sections can be considered subtassor functions
loo at each of the subtass and identify "ithin them furthersubtass! and so on
this process of identifying 'rst the major tass! then furthersubtass "ithin them! is no"n as top(do"n design or functionaldecomposition)
each of the subtass or functions "ill become a module "ithin asolution algorithm or program
-
7/25/2019 13 Modularization
4/39
Modularization
* module section of an algorithm
dedicated to a single function
maes the algorithm simpler more systematic
free of errors
Modularization is the process of dividing a probleminto separate tass! each "ith a single purpose)
Top(do"n design methodology allo"s theprogrammer to concentrate on the overall design ofthe algorithm "ithout getting too involved "ith thedetails of the lo"er(level modules)
-
7/25/2019 13 Modularization
5/39
Modularization
Modular name convention Describe the "or to be done as a single speci'c function
Use a verb! follo"ed by a t"o("ord object
E#ample+
$rint,page,heading
-alculate,sales,ta#
.alidate,input,date
The mainline
sho" the main processing functions! and the order in "hichthey are to be performed
sho" the /o" of data and the major control structures
easy to read! be of manageable length and sho" soundlogic structure
-
7/25/2019 13 Modularization
6/39
Modularization
0ene'ts of modular design Ease of understanding
each module should perform just one function)
1eusable code modules used in one algorithm can also be used in
others)
Elimination of redundancy
can help to avoid the repetition of "riting out thesame segment of code more than once)
E%ciency of maintenance each module should be self(contained and have
little or no e2ect on other modules "ithin thealgorithm)
-
7/25/2019 13 Modularization
7/39
$rocess three characters
Design a solution algorithm that "illprompt a terminal operator for threecharacters! accept those characters as
input! sort them into ascending se3uenceand output them to the screen) Thealgorithm is to continue to readcharacters until 45556 is entered)
Input Processing Output
char,7char,8char,9
$rompt for characters*ccept three characters&ort three charactersOutput three characters
char,7char,8char,9
Defning diagram
-
7/25/2019 13 Modularization
8/39
Process_three_charactersProm pt the operator for char_1, char_2, char_3Get char_1, char_2, char_3DO W HILE NOT (char_1 = ! "ND char_2 = ! "ND char_3 = !#
I$ char_1 % char_2 THEN tem p = char_1 char_1 = char_2 char_2 = tem p ENDI$ I$ char_2 % char_3 THEN tem p = char_2 char_2 = char_3
char_3 = tem p ENDI$ I$ char_1 % char_2 THEN tem p = char_1 char_1 = char_2 char_2 = tem p ENDI$ O &tp&t to the scree' char_1, char_2, char_3 Prom pt operator for char_1, char_2, char_3
Get char_1, char_2, char_3ENDD OEND
Solution algorithm
-
7/25/2019 13 Modularization
9/39
Process_three_charactersProm pt the operator for char_1, char_2, char_3Get char_1, char_2, char_3
DOW HILE NOT (char_1 = ! "ND char_2 = ! "ND char_3 = !# ort_three_characters O &tp&t to the scree' char_1,char_2,char_3 Prom pt operator for char_1, char_2, char_3 Get char_1,char_2,char_3ENDD OEND
Solution algorithm
ort_three_charactersI$ char_1 % char_2 THEN
tem p = char_1 char_1 = char_2 char_2 = tem pENDI$I$ char_2 % char_3 TH EN tem p = char_2 char_2 = char_3
char_3 = tem pENDI$I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$END
-
7/25/2019 13 Modularization
10/39
:ierarchy charts
:ierarchy charts 1epresentation of the functions or modules graphically
in a diagram)
It sho"s not only the names of all the modules butalso their hierarchical relationship to each other)
This diagrammatic form of hierarchical relationshipappears similar to an organizational chart of personnel"ithin a large company)
may also be referred to a structure chart
a visual table of contents)
-
7/25/2019 13 Modularization
11/39
Process_three_charactersProm pt the operator for char_1, char_2, char_3Get char_1, char_2, char_3
DOW HILE NOT (char_1 = ! "ND char_2 = ! "ND char_3 = !# ort_three_characters O &tp&t to the scree' char_1,char_2,char_3 Prom pt operator for char_1, char_2, char_3 Get char_1,char_2,char_3ENDD OEND
Solution algorithm
ort_three_charactersI$ char_1 % char_2 THEN
tem p = char_1 char_1 = char_2 char_2 = tem pENDI$I$ char_2 % char_3 TH EN tem p = char_2 char_2 = char_3
char_3 = tem pENDI$I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$END
-
7/25/2019 13 Modularization
12/39
$rocess three charactersControlling
moduleor
Calling moduleor
Mainline
Subordinatemodule
orCalled Module
-
7/25/2019 13 Modularization
13/39
Process_three_charactersProm pt the operator for char_1, char_2, char_3Get char_1, char_2, char_3DOW HILE NOT (char_1 = ! "ND char_2 = ! "ND char_3 = !# ort_three_characters O &tp&t to the scree' char_1,char_2,char_3 Prom pt operator for char_1, char_2, char_3 Get char_1,char_2,char_3ENDD OEND
Solution algorithm
ort_three_charactersI$ char_1 % char_2 THEN
tem p = char_1 char_1 = char_2 char_2 = tem pENDI$I$ char_2 % char_3 TH EN tem p = char_2 char_2 = char_3
char_3 = tem pENDI$I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$END
Controllingmodule
orCalling module
orMainline
Subordinatemodule
orCalled Module
-
7/25/2019 13 Modularization
14/39
$rocess three characters
Design a solution algorithm that "illprompt a terminal operator for threecharacters! accept those characters as
input! sort them into ascending se3uenceand output them to the screen) Thealgorithm is to continue to readcharacters until 45556 is entered)
Input Processing Output
char,7char,8char,9
$rompt for characters*ccept three characters&ort three charactersOutput three characters
char,7char,8char,9
Defning diagram
-
7/25/2019 13 Modularization
15/39
$rocess three characters
-
7/25/2019 13 Modularization
16/39
:ierarchy charts
-
7/25/2019 13 Modularization
17/39
ort_three_charactersI$ char_1 % char_2 THEN
tem p = char_1 char_1 = char_2 char_2 = tem pENDI$I$ char_2 % char_3 TH EN tem p = char_2 char_2 = char_3 char_3 = tem pENDI$I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$EN D
Process_three_characters)ea*_three_charactersDO W HILE NO T (char_1 = ! "ND char_2 = ! "ND char_3 = !#
ort_three_charactersPr+'t_three_characters)ea*_three_characters
ENDDOEND
)ea*_three_charactersProm pt the operator for char_1,char_2,char_3G et char_1, char_2, char_3END
Solution algorithm
Pr+'t_three_charactersO &tp&t to the scree' char_1, char_2, char_3
END
-
7/25/2019 13 Modularization
18/39
-ommunication bet"een
modules ;e should consider not only the division of
the problem into modules but also the /o"of informationbet"een the modules)
The fe"er and simpler the communicationsbet"een modules! the easier it is tounderstand and maintain one module"ithout reference to other modules)
This /o" of information! called intermodulecommunication! can be accomplished by thescope of the variable
-
7/25/2019 13 Modularization
19/39
&cope of a variable
The scope of a variable is the portion of a program in"hich that variable has been de'ned and to "hich itcan be referred)
Global
-
7/25/2019 13 Modularization
20/39
&ide e2ects
* side e2ect is a form of cross(communication of a module "ith other partsof a program)
It occurs "hen a subordinate module altersthe value of a global variable inside amodule)
&ide e2ects are not necessarily detrimental)
:o"ever! they do tend to decrease themanageability of a program)
-
7/25/2019 13 Modularization
21/39
$arameters
$arameters are simply data items transferredfrom a calling module to its subordinate moduleat the time of calling)
;hen a calling module calls a subordinatemodule in pseudocode! it must consist of thename of the called module "ith a list of theparameters to be passed to the called moduleenclosed in parentheses! e#ample+
$rint,page,headings
-
7/25/2019 13 Modularization
22/39
$arameters
?ormal parameters appear "hen a submodule is de'ned
*ctual parameters variables and e#pressions that are passed to a
submodule
?or e#ample a mainline may call a module "ith an actual
parameter list $rint,page,headings
-
7/25/2019 13 Modularization
23/39
$arameters
$arameters may have one of threefunctions+
To pass information from a calling module to
a subordinate module)To pass information from a subordinate
module to its calling module)
To ful'll a t"o("ay communication role)
-
7/25/2019 13 Modularization
24/39
$arameters
.alue parameters
only pass data one "ay
the called module may not modify the value
of the parameter "hen the submodule has 'nished processing!
the value of the parameter returns to itsoriginal value
1eference parameters 1eference parameters can pass data to a
called module "here that data may bechanged and then passed bac to the calling
module! the reference address of the
-
7/25/2019 13 Modularization
25/39
Increment t"o counters
Design an algorithm that "ill increment t"o counters fromto 7@ and then output those counters to the screen) Aourprogram is to use a module to increment the counters)
Defning diagram
Input Processing Output
counter7counter8
Increment countersOutput counters
counter7counter8
I'crem e't_t o_co&'terset co&'ter1, co&'ter2 to -eroDO I = 1 to 1.
I'crem e't_co&'ter(co&'ter1# I'crem e't_co&'ter(co&'ter2# O &tp&t to the scree' co&'ter1, co&'ter2ENDD OEND
I'crem e't_co&'ter (co&'ter#co&'ter = co&'ter / 1
END
Solution algorithm
-
7/25/2019 13 Modularization
26/39
:ierarchy charts and
parameters $arameters can be incorporated into a hierarchy
chart using the follo"ing symbols+
Data parameters contain the actual variables ordata items that "ill be passed bet"een modules)
&tatus parameters act as program /ags andshould contain just one of t"o values+ true or
false)
-
7/25/2019 13 Modularization
27/39
$rocess three characters
Design a solution algorithm that "ill prompt a terminal operatorfor three characters! accept those characters as input! sort theminto ascending se3uence and output them to the screen) The
algorithm is to continue to read characters until 45556 is entered)
Input Processing Output
char,7char,8char,9
$rompt for characters*ccept three characters&ort three charactersOutput three characters
char,7char,8char,9
Defning diagram
-
7/25/2019 13 Modularization
28/39
Group the activities intomodulesConstruct a hierarchy chart
-
7/25/2019 13 Modularization
29/39
ort_three_charactersI$ char_1 % char_2 THEN
tem p = char_1 char_1 = char_2 char_2 = tem pENDI$I$ char_2 % char_3 TH EN tem p = char_2 char_2 = char_3 char_3 = tem pENDI$I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$EN D
Process_three_characters)ea*_three_charactersDO W HILE NO T (char_1 = ! "ND char_2 = ! "ND char_3 = !#
ort_three_charactersPr+'t_three_characters
)ea*_three_charactersENDDO
END)ea*_three_charactersProm pt the operator for char_1,char_2,char_3G et char_1, char_2, char_3END
Solution algorithm
Pr+'t_three_charactersO &tp&t to the scree' char_1, char_2, char_3
END
-
7/25/2019 13 Modularization
30/39
ort_three_characters (char_1, char_2, char_3#I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$I$ char_2 % char_3 TH EN tem p = char_2 char_2 = char_3 char_3 = tem pENDI$I$ char_1 % char_2 TH EN tem p = char_1 char_1 = char_2 char_2 = tem pENDI$EN D
Process_three_characters)ea*_three_characters (char_1, char_2, char_3#DO W HILE NO T (char_1 = ! "ND char_2 = ! "ND char_3 = !# ort_three_characters (char_1, char_2, char_3# Pr+'t_three_characters (char_1, char_2, char_3#
)ea*_three_characters (char_1, char_2, char_3#ENDD OEND
)ea*_three_characters (char_1, char_2, char_3#Prom pt the operator for char_1,char_2,char_3Get char_1, char_2, char_3EN D
Solution algorithm
Pr+'t_three_characters (char_1, char_2, char_3#O &tp&t to the scree' char_1, char_2, char_3END
-
7/25/2019 13 Modularization
31/39
Group the activities intomodulesConstruct a hierarchy chart
-
7/25/2019 13 Modularization
32/39
ort_three_characters (char_1, char_2, char_3#I$ char_1 % char_2 TH EN ap_t o_characters (char_1, char_2#ENDI$I$ char_2 % char_3 TH EN
ap_t o_characters (char_2, char_3#ENDI$I$ char_1 % char_2 TH EN ap_t o_characters (char_1, char_2#ENDI$EN D
Process_three_characters)ea*_three_characters (char_1, char_2, char_3#DO W HILE NO T (char_1 = ! "ND char_2 = ! "ND char_3 = !# ort_three_characters (char_1, char_2, char_3# Pr+'t_three_characters (char_1, char_2, char_3#
)ea*_three_characters (char_1, char_2, char_3#ENDD OEND
)ea*_three_characters (char_1, char_2, char_3#Prom pt the operator for char_1,char_2,char_3G et char_1, char_2, char_3END
Solution algorithm
ap_t o_characters (f+rst_char, seco'*_char#
tem p = f+rst_charf+rst_char = seco'*_charseco'*_char = tem p
EN D
Pr+'t_three_characters (char_1, char_2, char_3#
O &tp&t to the scree' char_1, char_2, char_3END
-
7/25/2019 13 Modularization
33/39
&teps in modularization
7) De'ne the problem by dividing it into its threecomponents+ input! output and processing)
8) Group the activities into subtass or functions todetermine the modules that "ill mae up the program)
9) -onstruct a hierarchy chart to illustrate the modulesand their relationship to each other)
B) Establish the logic of the mainline of the algorithm inpseudocode)
C) Develop the pseudocode for each successive modulein the hierarchy chart)
) Des chec the solution algorithm)
-
7/25/2019 13 Modularization
34/39
-alculate employee6s pay
* program is re3uired by a company to read an employee6snumber! pay rate and the number of hours "ored in a "ee) Theprogram is then to validate the pay rate and the hours "ored'elds and! if valid! compute the employee6s "eely pay and printit along "ith the input data)
.alidation+ *ccording to the company6s rules! the ma#imum hoursan employee can "or per "ee is @ hours! and the ma#imumhourly rate is 8C)@@ per hour) If the hours "ored 'eld or thehourly rate 'eld is out of range! the input data and an appropriatemessage is to be printed and the employee6s "eely pay is not to
be calculated);eely pay calculation+ ;eely pay is calculated as hours "oredtimes pay rate) If more than 9C hours are "ored! payment forthe overtime hours "ored is calculated at time(and(a(half)
-
7/25/2019 13 Modularization
35/39
Defne the problem
Input Processing Output
emp,nopay,rate
hrs,"ored
1ead employee details.alidate input 'elds
-alculate employee pay$rint employee details
emp,nopay,rate
hrs,"oredemp,"eely,payerror,message
roup the activities into modules
Construct a hierarchy chart
bli h h l i ! h i li ! h l i h
-
7/25/2019 13 Modularization
36/39
ablish the logic o! the mainline o! the algorithm"g pseudocode
0omp&te_empoee_pa1 )ea*_empoee_*eta+s (empoee_*eta+s#2 DOWHILE more recor*s3 a+*ate_+'p&t_f+e*s (empoee_*eta+s, 4a+*_+'p&t_f+e*s#5 I$ 4a+*_+'p&t_f+e*s THEN 0ac&ate_empoee_pa (empoee_*eta+s# Pr+'t_empoee_*eta+s (empoee_*eta+s# ENDI$6 )ea*_empoee_*eta+s (empoee_*eta+s# ENDDO END
elop the pseudocode !or each successive modulehe hierarchy chart
)ea*_empoee_*eta+s (empoee_*eta+s#7 )ea* emp_'o, pa_rate, hrs_or8e*
END
-
7/25/2019 13 Modularization
37/39
a+*ate_+'p&t_f+e*s (empoee_*eta+s, 4a+*_+'p&t_f+e*s#9 et 4a+*_+'p&t_f+e*s to tr&e: et error_messa;e to
-
7/25/2019 13 Modularization
38/39
$roduct orders report
The *cme &pare $arts -ompany "ants to produce a product ordersreport from its product orders 'le) Each record on the 'le containsthe product number of the item ordered! the product description!the number of units ordered! the retail price per unit! the freightcharges per unit! and the pacaging costs per unit)
Aour algorithm is to read the product orders 'le! calculate the totalamount due for each product ordered and print these details onthe product orders report)
The amount due for each product is calculated as the product ofthe number of units ordered and the retail price of the unit) *
discount of 7@F is allo"ed on the amount due for all orders over7@@)@@) The freight charges and pacaging costs per unit mustbe added to this resulting value to determine the total amountdue)
-
7/25/2019 13 Modularization
39/39
$roduct orders report
The output report is to contain headings andcolumn headings as speci'ed in the follo"ing
chart+
Each detail line is to contain the product number!product description! number of units ordered andthe total amount due for the order) There is to be anallo"ance of BC detail lines per page)
#CM SP#$P#$%
O$D$S $PO$% P#G &&
$1ODU-TO
$1ODU-TDE&-1I$TIO
UIT&O1DE1ED
TOT*> *MOUTDUE
#### ########## ### #####
#### ########## ### #####