0% found this document useful (0 votes)
3 views57 pages

07 CSC415 Chapter 7 Modules

This document discusses algorithm design principles, specifically top-down and bottom-up approaches, and the use of modules to solve complex problems. It explains how to break down problems into smaller, manageable parts, create algorithms for each part, and how to pass information between modules using parameters. Additionally, it covers the structure of pseudocode for modular programming and the types of parameters used in module interactions.

Uploaded by

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

07 CSC415 Chapter 7 Modules

This document discusses algorithm design principles, specifically top-down and bottom-up approaches, and the use of modules to solve complex problems. It explains how to break down problems into smaller, manageable parts, create algorithms for each part, and how to pass information between modules using parameters. Additionally, it covers the structure of pseudocode for modular programming and the types of parameters used in module interactions.

Uploaded by

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

* Algorithm Fundamentals

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

Internet Banking Self Service


Banking

MyBank Transfer Pay Bill ATM CDM CQM


Account Funds

Withdraw Inquire Transfer Deposit Pay Credit Deposit Pay Cr Pay


Cash Balance Funds Cash Bill Card Cheque Card Loan
*
*Top Down Design (cont.)
*The advantages of implementing top down design are:
•Breaking the problem into parts or smaller problems helps us to
clarify what needs to be done.
•At each step of refinement, the new sub problems become less
complicated and therefore, it is easier to figure out and can be
easily documented and coded.
•Parts of the solution may turn out to be reusable.
•Breaking the problem into parts allows more than one person to
work on the solution.
*
*Bottom Up Design
* The same way will be implemented when we want to get the entire algorithm for a big
problem. What we have to do:
• Break down a big or complex problem into a few smaller problems.
• Try to get the solution for each small problem independently. Should any of these small
problems be broken down further? Ask yourself whether or not you could easily write the
algorithm for the small problem. If not, break it down again.
• When you are comfortable with the breakdown, write the algorithm for each of the small
problems.
• Combine all algorithms for each smaller problem to get a complete algorithm for an
entire problem.
* In this technique, we have to gather or integrate all the algorithms of all sub problems to
get the complete algorithm. This technique is called bottom up design.
*
Complex Problem Example of Bottom Up
Design
Sub-Problem Sub-Problem Sub-Problem
A B C

Algorithm A

Sub-Problem Sub-Problem
B1 B2

Algorithm B1 Algorithm B2 Sub-Problem


C1
A complete solution: Sub-Problem C2
Algorithm A + Algorithm B1 + Algorithm B2
+ Algorithm C1 + Algorithm C2
Algorithm C1 Algorithm C2
*
*The above diagram can be represented using organization chart
or hierarchy chart as follows:
Complex Problem

Sub-Problem A Sub-Problem B Sub-Problem C

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

Module A Module B Module C

The diagram shows the relationship between the main


module and the other modules. Each module may be
subdivided further.
*
Modules (cont.)
• With many modules, one major difference is that they can send
or receive value(s) from one module to another module.
• The main module is still the main program. All processing
started and completed in the main module.
• Sending and receiving value(s) causes the program flow to be
altered.
Understanding how and what it takes for these modules to pass
and return value(s) and how the program flow shifted will be the
major features in the following slides.
*
Skeleton for Pseudocode
As mentioned in Chapter 3, there are two skeletons to write an
algorithm. The first skeleton is used to solve a simple problem. The
syntax to use the first skeleton is as follows:
Main Module
Start
Action 1
Action 2
..
Action n
End
*
Module Subproblem 1( ) Module
Action 1.1 header
Action 1.2 module definition
Meanwhile the second ..
Action 1.t
skeleton is used to solve a Return Module
complex problem. The Module Subproblem 2( ) header
syntax to use the second Action 2.1
Action 2.2
skeleton is as follows: Calling
module .. module definition
statement Action 2.m
Main Module Return
Start ::
Call Subproblem 1( ) Module
Call Subproblem 2( ) Module Subproblem n( ) header
… Action p.1
Call Subproblem p( ) Action p.2
End .. module definition
Action p.s
Return
*
*Note :
• The main module is known as the calling module. The module(s) is
known as the called module.
• The statement Call Subproblem 1( ) , Call Subproblem 2( ) and
Call Subproblem p( ) are known as calling module statement.
• A module consists of a module header(with or without parameter)
and a body of module (a group of actions).
• A module definition describes the actions the module is doing.
• The program execution will start in the main module and it is
where it will end. But throughout its execution, the main module
may call other module(s) to get certain process done.
*
What is a Module?
A module contains a group of actions or statements
referred to by name. It is usually created to perform one
particular task. In relation to the top down design, a
module is usually the part that is at the base elements
where algorithm is more easily designed.
*
*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 performed inside the
module. For examples:
•findMax() is a module to find the maximum value.
•calculateAvg(), or calcAvg() or calcAverage() is a module
to calculate the average of N numbers.
•calTotal() is a module to compute a sum.
*
*Problem Solving Using Module(s)
*An example:
*
*Problem Solving Using Module(s) (cont.)
*Analysis :
The above diagram can be divided into two different diagrams as
follows:
Diagram 1: Diagram 2:
*******
$
$$
******* $$$
******* $$$$
*
*
*Step 3: Get detail processes or solution for each module as
follows:
*
* Step 4 : Combine all modules and the main module as follows:

Note: Notice how the main


module contains only module
calling statements.
*
*. i ii
The example shows how program
execution is carried out. The arrow
vi vii represents the program flow.
ix i. The algorithm first executes in
iii the main module.
ii. A calling module statement is
iv v found.
iii. Program flow is shifted to the
called module.
iv. The called module is executed.
viii v. The Return statement signals the
end of the execution and returns
the program flow back to the
main module.
vi. The main module executes again.
vii. A calling module statement is
found.
viii.Repeat similar program flow iii,
iv, v, vi.
ix. The main module reaches the
end of the execution. The
*
* The flowcharts for the above pseudocode are as follows:
*

1. The 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.

The actual parameters can also


be variable(s). For example,
Call drawDiagram(no1, no2,
shape)
Call drawDiagram(no2, no3,
diagram)
*
The flowhart for the above
pseudocode.
*
When to Use Parameter?
To determine when to use parameter(s) and which
type of parameter(s) to be used are important.

Three types of formal parameter:


i. in formal parameter
ii. out formal parameter
iii. in-out formal parameter
*
When to Use Parameter? (cont.)
Parameter is used in three situations:
i. Send data from calling module into called module. Use in
formal parameter.
ii. Send data from called module into calling module. Use out
formal parameter.
iii.Send data from calling module into called module. Do some
changes to the data and send the data back to the calling
module. Use in-out formal parameter.
A value formal parameter is used in situation (1). An
address/reference formal parameter is suitable for situations (2
and 3).
*
When to Use Parameter? (cont.)
*Sometimes a module does not need any parameter. This
happens when:
i. no data are passed from calling module to called module.
ii. in a situation where all data are needed to do activities in a
called module is already known.
iii.in a situation where all data are needed to do activities in a
called module is inside the module.
*
How Parameter Is Passed?
Two types of parameter passing are:
i. Value formal parameter. Use for in parameter. It means the content
or value of the parameter is passed to another parameter. The
actual and formal parameters are separate memory spaces and will
end up having the same exact copies of the values.
ii. Address/reference formal parameter. Use for out and in/out
parameter. It means the address of where the parameter is located
in the memory is passed to another parameter. Both the actual and
formal parameters will end up refering to the same memory
space and sharing the same value.
*
Passing Parameter by Value
Main Module Module Addition
Call Addition(no1, no2) Module Addition(in n1, in n2)

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) Module Addition(in n1, in n2)

Memory Memory Note:


•The arrow shows
no1 n1 data transfer from
the main module to
4 4 module Addition.
no2 n2
•Passing parameter
by value is a way for
10 10 the main module to
pass data to the
module(s).
*
Passing Parameter by Value (cont.)
Note:
• no1 and no2 are local to the main module. n1 and n2 are local
to module Addition. Local variables are only recognized inside
that module only.
• As a consequence, the main module can directly change no1 and
no2, but it cannot change n1 and n2. Likewise, module addition
can directly change n1 and n2 but it cannot change no1 and
no2.
• In short, any changes to no1 and no2 will not affect n1 and n2.
Likewise, any changes to n1 and n2 will not affect no1 and no2.
*
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
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.)

Module Exchange ( in x, in-out y )


Display “ in module Exchange “ , newline
Display “ x = “ , x , ‘’y = “ , y, newline
If ( x > y )
temporary = x
x=y
y = temporary
endIF
Display “After process in module “ , newline
,“x=“, x , “ y = “ , y, newline
Return
Note: The in-out indicates data is sent both ways.
*
Tracing (cont.)
Example 1
*
*
*Advantages Using Module
• A good practice of problem solving. Through this approach it is
easier to get the solution. When the solution contains any error,
it is easy to debug. Why? Because the debugging process only
focuses on one entire module where the errors exist.
• Module can be used repeatedly. The same module can be used
repeatedly at different part in the main module. This way can
make our solution shorter.
• The solution is more systematic.
*
*The end

You might also like