350 likes | 879 Views
8. First Steps in Modularization. Simple Program Design Third Edition A Step-by-Step Approach. 8. Objectives. To introduce modularization as a means of dividing a problem into subtasks To present hierarchy charts as a pictorial representation of modular program structure
E N D
8 First Steps in Modularization Simple Program Design Third Edition A Step-by-Step Approach
8 Objectives • To introduce modularization as a means of dividing a problem into subtasks • To present hierarchy charts as a pictorial representation of modular program structure • To discuss intermodule communication, local and global variables, and the passing of parameters between modules • To develop programming examples that use a simple modularized structure
8 Modularization • As programming problems increase in complexity, it becomes more and more difficult to consider the solution as a whole • You must identify the major tasks to be performed in the problem, then divide the problem into sections that represent those tasks • This process of identifying first the major tasks, then further subtasks within them, is known as top-down design or functional decomposition
8 Modularization • By using a top-down design methodology, the programmer is adopting a modular approach to program design • Modularization is the process of dividing a problem into separate tasks
8 The Modularization Process • When you are defining the problem, write down the activities or processing steps to be performed • The emphasis when defining the problem must still be to concentrate on the tasks or functions that need to be performed • A module must be large enough to perform its task, and must include only the operations that contribute to the performance of that task
8 The Modularization Process • The name of the module should describe the work to be done as a single specific function • The convention of naming a module by using a verb, followed by a two-word object, is particularly important, as it helps to identify the separate task or function that the module has been designed to perform • Typical module names might be: Print_page_headings Calculate_sales_tax Validate_input_date
8 The Mainline • A mainline routine must provide the master control that ties all the modules together and coordinates their activity • This program mainline should show the main processing functions, and the order in which they are to be performed • It should also show the flow of data and the major control structures
8 Example 8.1 Read Three Characters • Design a solution algorithm that will prompt a terminal operator for three characters, accept those characters as input, sort them into ascending sequence and output them to the screen. The algorithm is to continue to read characters until ‘XXX’ is entered • A Defining diagram (see figure at bottom of page 105 of the textbook)
B Initial solution algorithm Refer to the algorithm written out on page 106 of the textbook C Solution algorithm using a module Refer to the code written out on pages 106 and 107 of the textbook The solution algorithm now consists of two modules: a mainline module called Read_three_characters and a submodule called Sort_three_characters 8 Example 8.1 Read Three Characters
8 Hierarchy Charts or Structure Charts • After the tasks have been grouped into functions or modules, present these modules graphically in a diagram • This diagram is known as a hierarchy chart, as it shows not only the names of all the modules but also their hierarchical relationship to each other
8 Hierarchy Charts or Structure Charts • A hierarchy chart may also be referred to as a structure chart, or a visual table of contents • 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 other in the program hierarchy
8 Communication Between Modules • When designing solution algorithms, you should consider not only the breaking up of the problem into modules but also the flow of information between the 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
8 Scope of a Variable • The scope of a variable is the portion of a program in which that variable has been defined and to which it can be referred • If a list is created of all the modules in which a variable can be referenced, that list defines the scope of the variable • Variables can be global, where the scope of the variable is the whole program, and local, where the scope of the variable is simply the module in which it is defined
8 Global Data • When information or data uses the same variable name in both the calling module and the called modules of a program, the data is known as global data, and the scope of the variable is the whole program because the data can be accessed by every module in the program • All data, however, does not need to be global
8 Local Data • Variables that are defined within a subordinate module are called local variables • These local variables are not known to the calling module, nor to any other module
8 Side Effects • A side effect is a form of cross-communication of a module with other parts of a program • It occurs when a subordinate module alters the value of a global variable inside a module • Side effects are not necessarily detrimental
8 Passing Parameters • Another 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 • When a calling module calls a subordinate module in pseudocode, it must consist of the name of the called module with a list of parameters to be passed to the called module enclosed in parentheses
8 Passing Parameters • An example: Print_page_headings (page_count, line_count) • 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 fulfill a two-way communication role • Refer to the figures on page 110 for the symbols that can be incorporated into a hierarchy or structure chart
8 Passing Parameters • When designing modular programs, the programmer should avoid using data parameters to indicate status, because this can affect the program in two ways: 1. It may confuse the reader of the program because a variable has been overloaded 2. It may cause unpredictable errors when the program is amended at some later data, as the maintenance programmer may be unaware of the dual purpose of the variable
8 Using Parameters in Program Design • Refer to Example 8.1 and change the solution algorithm so that parameters are used to communicate between modules as shown in Example 8.2 of the textbook • A Defining diagram (see the figure at the top of page 111 of the textbook) • B Group the activities into modules 1. Read_three characters 2. Sort_three characters
8 Using Parameters in Program Design • C Construct a hierarchy chart • Refer to the figure at the bottom of page 111 of the textbook for an example • D Establish the logic of the solution algorithm using pseudocode (mainline and subordinate module) • Have students refer to page 112 of the textbook to review the pseudocode
8 Steps in Modularization • Top-down modular design is really quite simple if the following steps are performed every time you are presented with a programming problem: 1. Define the program 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
8 Steps in Modularization 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 5. Develop the pseudocode for each successive module in the hierarchy chart 6. Desk check the solution algorithm. This is achieved by first desk checking the mainline, then each subordinate module in turn
8 Programming Examples Using Modules • Example 8.3 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 compute the employee’s weekly pay and print it along with the input data • A) Define the problem (see the figure at the top of page 114 of the textbook)
8 Programming Examples Using Modules • B) Group the activities into modules • The activities can be grouped into three main functions: 1. Compute_employee_pay 2. Validate_input_fields 3. Calculate_employee_pay • C) Construct a hierarchy chart (see the figure on page 114 of the textbook) • If you write the solution algorithm as three separate modules, the readability of the solution will improve dramatically
8 Programming Examples Using Modules • D) Establish the logic of the mainline of the algorithm using pseudocode • Do this by using code as shown on page 115 of the textbook • E) Develop the pseudocode for each successive module in the hierarchy chart • Accomplish this task by referring to the code shown on page 115 of the textbook
8 Programming Examples Using Modules • F) Desk check the solution algorithm • Use the following steps to do this: 1. Create some valid input test data 2. List the output that the input data is expected to produce 3. Use a desk check table to walk the data through the mainline of the algorithm to ensure that the expected output is achieved
8 Programming Examples Using Modules (i) Input data: (see the figure on page 116 of the textbook) Three test cases will be used to test the algorithm (ii) Expected results Refer to the examples shown on page 116 of the textbook (iii) Desk check table (see the figure on page 117 of the textbook)
8 Summary • A module is a section of an algorithm that is dedicated to the performance of a single function • Top-down design is the process of dividing a problem first into the major tasks and then into further subtasks within those major tasks until all the tasks have been identified • Hierarchy charts are a method of illustrating the structure of a program that contains modules
8 Summary • Intermodule communication is the flow of information or data between modules • The steps in modularization that a programmer must follow are: • Define the problem • Group the activities into subtasks or functions • Construct a hierarchy chart • Establish the logic of the mainline, using pseudocode • Develop the pseudocode for each successive module in the hierarchy chart • Desk check the solution algorithm