07 CSC415 Chapter 7 Modules
07 CSC415 Chapter 7 Modules
Chapter 7: Modules
*
*Overview
*This lesson covers the following topics:
•Understand what does it mean by top down and bottom up
design
•Apply top down and bottom up approach in real world problem
•Construct algorithm to solve problem modularly
•Apply modular mechanism to pass information between modules
•Apply searching algorithm
•Understand sorting algorithm
*
*Introduction
* A problem can have simple solution or complex solution. It depends on the extent of the
problem itself. To find the average of ten numbers requires a simple solution. To update the
balance of a bank account will require a more complex solution.
* But regardless of the extent of the problem, a solution can be designed. If a problem is too
complex, one way is to break it apart into smaller parts. If the part is still too large, break it
further until each part is reduced to base elements in which each part can be solved with a
simple algorithm. We will end up having many small algorithms.
* In programming, this technique of breaking complex problem into smaller sub problem is
called top down design or stepwise refinement.
*
*Top Down Design
Banking
Algorithm A
Sub-Problem Sub-Problem
B1 B2
Sub-Problem B1 Sub-Problem B2
Sub-Problem C1 Sub-Problem C2
*
*Stub
*In some circumstances, we found that we do not yet completed the
algorithm for a certain part or sub problem. But, we need to perform
program testing. To overcome this problem, we have to create a stub to
replace the incomplete algorithm. The purpose of the stub is to make
sure that we can execute the entire solution.
*
*For example, let say we break the problem into three parts, X, Y and Z.
We completed the algorithm for X and Y. It means that we cannot
integrate all the solutions because of an incomplete algorithm for Z. To
test that the algorithm for X and Y are correct, we have to replace the
algorithm for part Z with a stub, then we can test our entire solution.
*
Modules
For simple problem, the solution is usually contained in
the main module. But, for complex problem, the solution
may be subdivided into the main module and separate
sub modules. Main Module
2. Module Diagram1()
*
3. Module Diagram2()
*
*How to Name a Module?
*There is no specific way to name a module. It is similar
as how to name a variable. The name of a module
should reflect what is actually done inside the module.
For examples:
•A module to find the maximum value, then the
suggested name is findMax().
•A module to calculate the average of N numbers, the
suggested names are calculateAvg(), or calcAvg() or
calcAverage().
*
Problem Solving Using Module(s)
To write a pseudocode that uses module(s), follow the
steps below:
i. Write a basic algorithm.
ii. Convert a basic algorithm as a main module. Identify
simple versus complex process.
iii.Convert each process (complex or relatively
complex ) into a module.
iv.Do not convert a simple process into a module.
*
Problem Solving Using Module(s) (cont.)
The example below demonstrates the steps above.
Suppose a problem is to read three numbers. Find the biggest and
the smallest number. Then sort the numbers in ascending order.
i. Write basic algorithm
Start
Read 3 numbers
Find the biggest number
Find the smallest number
Sort numbers in ascending order
Display the biggest, the smallest, numbers in ascending order
End
*
Problem Solving Using Module(s) (cont.)
ii. Convert a basic algorithm as a main module. Identify
simple versus complex process.
-Let the process Read 3 numbers in the main module.
-Let the process Display the biggest, the smallest, numbers in
ascending order in the main module.
*
Problem Solving Using Module(s) (cont.)
iii. Convert each process (complex or relatively
complex) into a module.
iv. Do not convert a simple process into a module.
Suggested:
-Do the following processes in three different modules:
• Find the biggest number
• Find the smallest number
• Sort numbers in ascending order
*
Problem Solving Using Module(s) (cont.)
Suggested new algorithm:
Main Module
Start
Read 3 numbers
Call findBiggest()
Call findSmallest()
Call sortNumber()
Display the biggest, the smallest, numbers in ascending order
End
*
What is a Parameter?
As mentioned earlier, a main module may send value(s)
to module(s) or vice versa.
When passing or receiving value(s), a parameter is
needed to hold these values. A parameter is just like a
variable, it has a name and it is used to hold values. But
it is used exclusively for data passing between modules.
*
What is a Parameter?(cont.)
Parameter types:
i. Formal parameter- located in the module header
ii. Actual parameter- located at the calling module
statement in the main module.
*
An example:
Module drawDiagram( in row, in column, in
Actual symbol )
* Main Module Parameter Set r to 1
* Start s
* Call drawDiagram(3, 4, ‘*’ ) While ( r ≤ row )
Set c to 1
While ( c ≤ column ) Formal Parameters
* Call drawDiagram(2, 7, ‘$’ )
Display symbol
Add 1 to c
* Call drawDiagram(4, 10, ‘@’ )
EndWhile
* End
Add 1 to r
Display newline
EndWhile
Return
*
What is a Parameter? (cont.)
Note: The term in before parameter name row, column and
symbol is to indicate that row, column and symbol will receive
values supplied by the main module.
The row, column and symbol in the Module drawDiagram( ) are
called formal parameters.
On the other hand, the values that the main module supplies to
drawDiagram() are called actual parameters.
*
What is a Parameter? (cont.)
• The first call to module drawDiagram(), the actual parameters are 3, 4,
‘*’.
• The second time call to module drawDiagram(), the actual parameters are
2, 7, ‘$’.
• The third time call to module drawDiagram(), the actual parameters are
4, 10, ‘@’.
When using actual and formal parameter(s), the number of them must be
equal. When the number of formal parameter is m, then the number of
actual parameter is also m. Parameter is also known as an argument.
Note: The main module can call the same module many times or it can call
different modules. The actual parameters can also be variable(s).
*
The relationship between the
main module and the module
drawDiagram(). The diagram
shows how actual parameter is
passed to the corresponding
formal parameter.
Memory Memory
no1 n1
no2 n2
* Note:
• no1, no2, n1 and n2 maintain separate memory spaces.
• Assume no1 is 4 and no2 is 10. When the algorithm is executed, 5 and 10 are passed to n1
and n2 as shown next.
*
Passing Parameter by Value (cont.)
Main Module Module Addition
Call Addition(no1, no2, average) Module Addition(in n1, in n2, out avg)
avg = n1 + n2
Return
Memory Memory
no1 n1
Note:
4 4 • no1, no2, n1 and n2
no2 n2 maintain separate
memory spaces as
10 10 before.
average avg • However, average and
avg refer to the same
memory space indicated
by the dash red arrow.
*
Passing Parameter by Address/Reference
Main Module Module Addition
Call addition(no1, no2, average) Module addition(in n1, in n2, out avg)
avg = n1 + n2
Return
Note:
Memory Memory • When module Addition is
no1 n1 executed, avg will
contain 7 and then 7 will
4 4 be sent to average.
no2 n2 • The passing parameter
by address/reference is a
10 10 way for the module(s) to
average avg transfer or pass data to
the main module.
7 7
*
Passing Parameter by Address/Reference (cont.)
Note:
• When the calling statement is executed, the address that holds the location of
average is passed to avg.
• average and avg end up refering to the same memory space as shown above.
• Because of this sharing of the memory space, both modules can update the same
location. If the main module changes average, avg will be affected or vice versa.
• This is a major difference between value parameter passing and
address/reference parameter passing.
• But, the local variable concept is still preserved. The main module refers to the
location as average and the addition module refers to the location by avg.
*
More examples of problem solving using modules(s).
Example 1
The entire solution consists of three
parts, Main module, calculateF()
module and calculateG() module.
• Module calculateF() uses variables
nom and valueF.
• Module calculateG() uses variables
number and valueG.
• The main module uses variables F, G
and H.
• The value of F in the main module is
gained from calculateF() module
which is valueF.
• The value of G in the main module is
gained from calculateG() module
which is valueG.
*
Note: There is no link
between variable nom
inside the module
calculateF() and the main
module. It is similar for
variable number inside the
module calculateG(). These
variables are known as local
variable. The local variables
are only recognized inside
that modules only i.e. nom is
recognized in module
calculateF() only and
number is recognized in
module calculateG() only.
The diagram that shows the relationship
between the main module and the modules.
*
Example 2
*
*
*
Tracing
Example 1
Given the following pseudocode. What is the output when the
data entered are 25 and 10.
Main Module
Start
Read number1, number2
Display “ values in main module before call module “ , newline
Display “ number1 = “ , number1, “ number 2 =” , number2, newline
Call Exchange (number1, number2 )
Display “ values in main after call module“ , newline
Display “ number1 = “ , number1, “ number 2 = “ , number2
End
*
Tracing (cont.)