Edited 06 - Pseudocode Modular

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

INF621102 – BASICS

PROGRAMMING Modular Pseudocode

WEEK 6
MODULARIZATION
As the complexity of the programming problems increases, however, it becomes
more and more difficult to consider the solution as a whole.
When presented with a complex problem, you may need to divide the problem into
smaller parts.
Modularization is the process of dividing a problem into separate tasks, each with a
single purpose.
BENEFITS OF MODULAR
DESIGN
Ease of understanding
 Each module should perform just one function.

Reusable code
 Modules used in one program can also be used in other programs.

Elimination of redundancy
 Using modules 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
program
EXAMPLE 1: PROCESS THREE
CHARACTERS
Design an algorithm that will prompt a terminal operator for three characters, accept
those characters as input, sort them into ascending and output them to the screen. The
algorithm is to continue to read characters until ‘XXX’ is entered.
EXAMPLE 1: PROCESS THREE
CHARACTERS
Defining diagram
EXAMPLE 1: PROCESS
THREE CHARACTERS

Original Solution Algorithm


EXAMPLE 1: PROCESS
THREE CHARACTERS
Solution Algorithm using a
module
HIERARCHY CHARTS
OR
STRUCTURE CHART
HIERARCHY CHART OR
STRUCTURE CHART
Once the tasks have been grouped into functions or modules, these modules can be
represented graphically in a diagram.
This diagram is known as a hierarchy chart, as it shows not only name of all the
modules but also their hierarchical relationship to each other.
The hierarchy chart uses a tree-like diagram of boxes
Each box represents a module in the program and the lines connecting the boxes
represent the relationship of the modules to others in the program hierarchy.
EXAMPLE OF HIERARCHY
CHART
The hierarchy chart for Example 1: Process three characters is
relatively simple.
It shows a calling module (Process_three_characters) and a
called module (Sort_three_characters)
EXAMPLE 2: PROCESS THREE
CHARACTERS
Example 1 could also have been design to use a
mainline and three modules, one for each of main
processing steps in the defining diagram
EXAMPLE 2: PROCESS THREE
CHARACTERS
Hierarchy chart
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/flowchart.
5. Develop the pseudocode/flowchart for each successive module in the hierarchy
chart.
6. Desk check the solution algorithm.
COMMUNICATION
BETWEEN MODULES
COMMUNICATION BETWEEN
MODULES
When designing solution algorithms, it is necessary to 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
 Is the portion of a program in which that variable has been defined and to which it can be referenced.

Global data
 Is data that can be used by all the modules in a program.
 The scope of a global variable is the whole program

Local data
 Variables that are defined within a submodule
 The scope of a local variable is limited to the execution of the single submodule in which it is defined
PASSING PARAMETERS
A particularly efficient method of intermodule communication is the passing of
parameters or arguments between modules.
Parameters are simply data items transferred from a calling module to its
subordinate module at the time of calling.
To pass parameters between modules, 2 things must happen:
 The calling module must name the parameters that it wants to pass to the submodule, at the time of
calling.
 The submodule must be able to receive those parameters and return them to the calling module, if
required.
PASSING PARAMETER (CONT.)
In pseudocode and most programming languages, when a calling module wants to pass
parameters to a submodule, it simply list the parameters, enclosed in parentheses, beside
the name of the submodule.

• The submodule must be able to receive those parameters, so it, too,


lists the parameters that it expects to receive, enclosed in parentheses,
beside the submodule name when it is defined.
VALUE AND REFERENCE
PARAMETERS
Parameters may have one of three functions:
1. To pass information from a calling module to a subordinate module.
2. To pass information from a subordinate module to its calling module.
3. To fulfil a two-way communication role.

Value parameters
 Pass a copy of the value of a parameter from one module to another.
 This form of parameter passing is called ‘passing by value’.

Reference parameters
 Pass the memory address of a parameter from one module to another.
 This form of parameter passing is called ‘passing by reference’.

The requirements of the program will determine whether a parameter is passed by value or
by reference.
EXAMPLE 1: CALCULATE
PERCENTAGE VALUE
Design an algorithm that will receive a fraction in the form of a numerator and a
denominator, convert that fraction to a percentage and display the result. Your
program is to use a module to calculate the percentage.
EXAMPLE 1: CALCULATE
PERCENTAGE VALUE
Defining diagram
EXAMPLE 1: CALCULATE
PERCENTAGE VALUE
Solution algorithm
EXAMPLE 2: INCREMENT TWO
COUNTERS
Design an algorithm that will increment two counters from 1 to 10 and then output
those counters to the screen. Your program is to use a module to increment the
counters.
EXAMPLE 2: INCREMENT TWO
COUNTERS
Defining diagram
EXAMPLE 2: INCREMENT TWO
COUNTERS
Solution algorithm
HIERARCHY CHART
AND PARAMETERS
HIERARCHY CHARTS AND
PARAMETERS
Parameters that pass between modules can be
incorporated into a hierarchy chart or structure chart
using the following symbols:
Data parameters contain the actual variables or
data items that will be passed as parameters between
modules.
Status parameters act as program flags and should
contain just one of two values: true or false.
EXAMPLE : CALCULATE
EMPLOYEE’S PAY
A program is required by a company to read an employee’s number, pay rate and the
number of hours worked in a week. The program is then to validate the pay rate field
and the hours work field and, if valid, compute the employee’s weekly pay and then
print it and the input data.
Validation: According to the company’s 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 are to be printed and the employee’s 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.
EXAMPLE : CALCULATE
EMPLOYEE’S PAY
Defining diagram
EXAMPLE : CALCULATE
EMPLOYEE’S PAY
Hierarchy chart
EXAMPLE : CALCULATE
EMPLOYEE’S PAY

Solution algorithm
PROGRAMMING PROBLEMS 1
Design an algorithm in modular pseudocode that will receive two integer items from
a terminal operator, and display to the screen their sum, difference, product, and
quotient. Note that the quotient calculation (first integer divided by second integer) is
only to be performed if the second integer does not equal zero.
PROGRAMMING PROBLEMS 2
Design an algorithm in modular pseudocode that will prompt an operator for a
student’s serial number and the student’s exam score out of 100. Your program is then
to match the exam score to a letter grade and print the grade to the screen. Calculate
the letter grade as follows:
PROGRAMMING PROBLEMS 3
Design an algorithm in modular pseudocode that will prompt a terminal operator for the
price of an article and a pricing code. Your program is then to calculate a discount rate
according to the pricing code and print to the screen the original price of the article, the
discount amount, and the new discounted price. Calculate the pricing code and
accompanying discount amount as follows:

If the pricing code is Z, the words ‘No discount’ are to be printed on the screen. If the
pricing code is not H, F, T, Q, or Z, the words ‘Invalid pricing code’ are to be printed.

You might also like