Edited 06 - Pseudocode Modular
Edited 06 - Pseudocode Modular
Edited 06 - Pseudocode Modular
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
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.
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.