0% found this document useful (0 votes)
2 views20 pages

Unit 4

The document introduces modularization in structured program design, emphasizing the importance of breaking down programs into manageable units called modules. It explains how modularization facilitates abstraction, collaboration among programmers, and code reuse, while providing examples of module interactions through pseudocode and flowcharts. Additionally, it includes class exercises for writing pseudocode and flowcharts for sorting numbers and generating Fibonacci sequences.
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)
2 views20 pages

Unit 4

The document introduces modularization in structured program design, emphasizing the importance of breaking down programs into manageable units called modules. It explains how modularization facilitates abstraction, collaboration among programmers, and code reuse, while providing examples of module interactions through pseudocode and flowcharts. Additionally, it includes class exercises for writing pseudocode and flowcharts for sorting numbers and generating Fibonacci sequences.
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/ 20

BIT 157

INTRODUCTION TO STRUCTURED
PROGRAM DESIGN

LINDA AMOAKO BANNING, PhD


UNDERSTANDING
MODULARISATION
MODULARISATION
● Breaking down programs into reasonable
units that tackle one small task at a time.
● Also referred to as
○ Subroutines
○ Procedures
○ Functions
○ Methods
WHY MODULARISATION
● It provides abstraction
● It allows multiple programmers to work on
a problem
● It allows the reuse of modules created
● It makes identifying structures easier
MODULARISING A PROGAM
● Most programs contain a main module
which contains the mainline logic
● This module accesses other modules
● Every module created must be named
○ One word which has a meaning followed by
a parenthesis
■ sum(), averageNumber(), taxpaid()
MODULARISING A PROGAM
● When a program uses another module, the
main program is said to be the calling
program (caller) and the module being used
is called the called program (callee)
● The flowchart symbol for calling a module is
a rectangle with double lines on its sides.
○ The name of the module being called is placed inside
the rectangle.
MODULARISING A PROGAM
● Each module is drawn separately with its own
sentinel symbols
● The start symbol will contain the name of the
module (this name must be identical to the name
used to call it)
● The symbol equivalent to stop contains a less final
term like exit or return.
● Control is returned to the calling program after a
module finishes executing
MODULARISING A PROGAM - EXAMPLE
● Write a program that calculates the arithmetic
average of two numbers
● Possible modules
○ The main program – this will call and stop other modules
○ Module to get the input (eg: getInput( ))
○ Module to calculate the average (eg: calculateAverage( ))
○ Module to print or output the results (eg: printResult( ))
MODULARISING A PROGAM - EXAMPLE
● The logic proceeds as follows:
○ Main program starts
○ Main program calls the getInput() module
■ Within the getInput( ) module
● a prompt appears for user to enter a number. We save this in a
variable firstNum
● Another prompt will appear for another number. Save this in a
variable secondNum
● getInput( ) ends
● Control is returned to the main program
MODULARISING A PROGAM - EXAMPLE
● The logic continues as follows:
○ Main program calls the calculateAverage( ) module
■ Within the calculateAverage( ) module
● firstNum and secondNum are added into a variable sum
● sum is divided by two and the result stored in a variable average
● The calculateAverage( ) module ends
● Control is returned to the main program
A PSEUDOCODE OF THE EXAMPLE
start calculateAverage( )
perform getInput( ) sum=(firstNum+secondNum)
perform calculateAverage( )
average = sum / 2
perform printResult( )
stop return
getInput( ) printResult( )
print “Enter a number” print average
read firstNumber print “Thank you”
print “Enter another number”
read secondNumber return
return
A FLOWCHART OF THE EXAMPLE
start getInput( )
calculateAverage( ) printResult( )

print
getInput( ) “Enter a
number” print
sum = (firstNum average
+ secondNum)
calculateAv
erage( ) read
firstNumber print
“Thank You”
printResult
() average = sum/2
print “Enter
another
number” return
stop

read return
secondNumbe
r

return
MODULES CALLING OTHER MODULES
● Any module can call another module, just as a program can call
a module or subroutine
● Example
○ Main program calls the getInput( ) module
○ The getInput( ) module calls the getFirstValue( ) module
○ The getFirstValue( ) displays a prompt and reads a number
○ getFirstValue( ) ends, control is passed back to getInput( ), where
getSecondValue( ) is called
○ getSecondValue( ) displays a prompt, and reads a second value from user
○ getSecondValue( ) ends and control is passed back to getInput( )
○ getInput( ) ends and control is passed back to the main program
○ calculateAverage( ) and printResult( ) execute as before
PSEUDOCODE OF MODULES CALLING
OTHER MODULES
start getSecondValue( )
perform getInput( ) print “Enter another number”
perform calculateAverage( ) read secondNum
perform printResult( ) return
stop
calculateAverage( )
getInput( ) sum = firstNum + secondNum
perform getFirstValue( ) average = sum / 2
perform getSecondValue ) return
return
printResult( )
getFirstValue( )
print average
print “Enter a number”
read firstNum print “Thank you”
return
return
SPEED BYTE
● Draw the Psuedocode from the
example of modules calling other
modules.
DECLARING VARIABLES IN PSEUDOCODE
● Variables are declared as the first step in the
program before you use any of them
● They appear to the side of the “declare variables”
step in an annotation symbol or annotation box,
which is simply an attached box containing notes.
● Annotation symbols can be used anytime you have
more to write than you can conveniently fit within a
flowchart
DECLARING VARIABLES IN PSEUDOCODE
Class Exercise Group 1
● Write the Pseudocode for a program that
accepts 5 numbers and sorts the numbers in
descending order.
● Draw the associated flowchart
Class Exercise Group 2
● Write the Pseudocode for a program that
accepts a number N from the user and
prints the first N terms of the Fibonacci
sequence.
● Draw the associated flow chart
END

You might also like