Presentation is loading. Please wait.

Presentation is loading. Please wait.

First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.

Similar presentations


Presentation on theme: "First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization."— Presentation transcript:

1 First Steps in Modularization

2 Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization as a means of dividing a problem into subtasks Present hierarchy charts as a pictorial representation of modular program structure Discuss intermodule communication, local and global variables, and the passing of parameters between modules Develop programming examples that use a simple modularized structure

3 Simple Program Design, Fourth Edition Chapter 8 3 Modularization is the process of dividing a problem into separate tasks, each with a single purpose Top-down design methodology allows the programmer to concentrate on the overall design of the algorithm without getting too involved with the details of the lower-level modules Modularization

4 Simple Program Design, Fourth Edition Chapter 8 4 The division of a problem into smaller subtasks, or modules, is a relatively simple process When you are defining the problem, write down the activities or processing steps 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 The Modularization Process

5 Simple Program Design, Fourth Edition Chapter 8 5 A module should have a single entry and a single exit with a top-to-bottom sequence of instructions 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 here, as it helps to identify the separate task or function that the module is to perform The Modularization Process

6 Simple Program Design, Fourth Edition Chapter 8 6 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 The mainline should be easy to read, be of manageable length, and show sound logic structure

7 Simple Program Design, Fourth Edition Chapter 8 7 Benefits of Modular Design There are a number of benefits from using modular design –Ease of understanding –Reusable code –Elimination of redundancy –Efficiency

8 Simple Program Design, Fourth Edition Chapter 8 8 Example 8.1 Process 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 (shown on page 111) InputProcessOutput char_1 char_2 char_3 Prompt for characters Accept three characters Sort three characters Output three characters char_1 char_2 char_3

9 Simple Program Design, Fourth Edition Chapter 8 9 Example 8.1 Process Three Characters B Original Solution Algorithm Process_three_characters Prompt the operator for char_1, char_2, char_3 Get char_1, char_2, char_3 DOWHILE NOT (char_1 = ‘X’ ANS char_2 = ‘X’ AND char_3 = ‘X’) IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF IF char_2 > char_3 THEN temp = char_2 char_2 = char_3 char_3 = temp ENDIF IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF Output to the screen char_1, char_2, char_3 Prompt the operator for char_1, char_2, char_3 Get char_1, char_2, char_3 ENDDO END

10 Simple Program Design, Fourth Edition Chapter 8 10 Example 8.1 Process Three Characters C Solution Algorithm Using a Module The solution algorithm now consists of two modules: a mainline module called Process_three_characters and a submodule called Sort_three characters When the mainline wants to pass control to its submodule, it simply names the module

11 Simple Program Design, Fourth Edition Chapter 8 11 Example 8.1 Process Three Characters B Solution Algorithm Using a Module Process_three_characters Prompt the operator for char_1, char_2, char_3 Get char_1, char_2, char_3 DOWHILE NOT (char_1 = ‘X’ ANS char_2 = ‘X’ AND char_3 = ‘X’) Sort_three_characters Output to the screen char_1, char_2, char_3 Prompt the operator for char_1, char_2, char_3 Get char_1, char_2, char_3 ENDDO Sort_three_characters IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF IF char_2 > char_3 THEN temp = char_2 char_2 = char_3 char_3 = temp ENDIF IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF END

12 Simple Program Design, Fourth Edition Chapter 8 12 Hierarchy Charts or Structure Charts After the program 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 A hierarchy chart may also be referred to as a structure chart or a visual table of contents as shown at the top of page 114 of the text 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

13 Simple Program Design, Fourth Edition Chapter 8 13 Example 8.2 Process 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 Ex. 8.1)

14 Simple Program Design, Fourth Edition Chapter 8 14 Example 8.2 Process Three Characters B Solution Algorithm The processing steps in the diagram on page 115 can be divided into three separate tasks, each task becoming a module in the algorithm These tasks are: Read three characters, Sort three characters, and Print three characters

15 Simple Program Design, Fourth Edition Chapter 8 15 Example 8.2 Process Three Characters B Solution Algorithm Process_three_characters Read_three_characters DOWHILE NOT (char_1 = ‘X’ ANS char_2 = ‘X’ AND char_3 = ‘X’) Sort_three_characters Print_three_characters Read_three_characters ENDDO END

16 Simple Program Design, Fourth Edition Chapter 8 16 Example 8.2 Process Three Characters Read_three_characters Prompt the operator for char_1, char_2, char_3 Get char_1, char_2, char_3 END Print_three_characters Output to the screen char_1, char_2, char_3 END Sort_three_characters IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF IF char_2 > char_3 THEN temp = char_2 char_2 = char_3 char_3 = temp ENDIF IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF END

17 Simple Program Design, Fourth Edition Chapter 8 17 Example 8.2 Process Three Characters Hierarchy Chart Process_three_characters Read_three_charactersSort_three_charactersPrint_three_characters

18 Simple Program Design, Fourth Edition Chapter 8 18 Communication Between Modules When designing solution algorithms, you should 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

19 Simple Program Design, Fourth Edition Chapter 8 19 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

20 Simple Program Design, Fourth Edition Chapter 8 20 Global Data 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, because every module in the program can access the data

21 Simple Program Design, Fourth Edition Chapter 8 21 Local Data Variables that are defined within a subordinate module are called local variables These local variables are not known to the calling module, or to any other module The scope of a local variable is simply the module in which it is defined

22 Simple Program Design, Fourth Edition Chapter 8 22 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 However, they do tend to decrease the manageability of a program

23 Simple Program Design, Fourth Edition Chapter 8 23 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 When the subordinate module terminates and returns control to its caller, the values in the parameters are transferred back to the calling module This method of communication avoids any unwanted side effects

24 Simple Program Design, Fourth Edition Chapter 8 24 Formal and Actual Parameters Parameter names that appear when a submodule is defined are known as formal parameters Variables and expressions that are passed to a submodule in a particular call are called actual parameters A call to a submodule will include an actual parameter list, one variable for each formal parameter name There is one-to-one correspondence between formal and actual parameters, which is determined by the relative position in each parameter list

25 Simple Program Design, Fourth Edition Chapter 8 25 Formal and Actual Parameters E.g. A mainline may call a submodule as follows: Print_page_headings (page_count, line_count) These actual parameters The submodule being called may have been declared as follows: Print_page_headings (page_counter, line_counter) These are formal parameters Note the parameter list are different between the actual and formal but they correspond

26 Simple Program Design, Fourth Edition Chapter 8 26 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 fulfill a two-way communication role

27 Simple Program Design, Fourth Edition Chapter 8 27 Value and Reference Parameters Parameters may have one of three functions: Calling Module Called Module Calling Module Called Module Calling Module Called Module 1. 2. 3.

28 Simple Program Design, Fourth Edition Chapter 8 28 Value Parameters Value parameters only pass data one way, the value of the parameter is passed to the called module When a subordinate is called, each actual parameter is assigned into the corresponding formal parameter, and from then on, the two parameters are independent

29 Simple Program Design, Fourth Edition Chapter 8 29 Reference Parameters Reference parameters can pass data to a called module where that data may be changed and then passed back to the calling module, the reference address of the parameter is passed to the called module Each actual parameter is an ‘alias’ for the corresponding formal parameter; the two parameters refer to the same object, and changes made through one are visible through the other As a result, the value of the parameter may be referenced and changed during the processing of the submodule

30 Simple Program Design, Fourth Edition Chapter 8 30 Example 8.3 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 A Defining Diagram (shown on page 119) InputProcessOutput counter1 counter2 Increment counters Output counters counter1 counter2

31 Simple Program Design, Fourth Edition Chapter 8 31 Example 8.3 Increment Two Counters B Solution Algorithm Increment_two_counters Set counter1, counter2 to zero DO I = 1 To 10 Increment_counter (counter1) Increment_counter (counter2) ENDDO END Increment_counter (counter) counter = counter + 1 END

32 Simple Program Design, Fourth Edition Chapter 8 32 Hierarchy Charts and Parameters Parameters, which pass between modules, can be incorporated into a hierarchy chart or structure chart using the symbols Data parameters contain the actual variables or data items that will be passed between modules Status parameters act as program flags and should contain just one of two values: true or false

33 Simple Program Design, Fourth Edition Chapter 8 33 Hierarchy Charts and Parameters For data parameters For status parameters

34 Simple Program Design, Fourth Edition Chapter 8 34 Hierarchy Charts and Parameters When designing modular programs, the programmer should avoid using data parameters to indicate status as well, 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 date, as the maintenance programmer may be unaware of the dual purpose of the variable

35 Simple Program Design, Fourth Edition Chapter 8 35 Using Parameters in Program Design Look again at Example 8.2 and change the solution algorithm so that parameters are used to communicate between modules

36 Simple Program Design, Fourth Edition Chapter 8 36 Example 8.4 Process 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 8.2) B Group the Activities into Modules A module will be created for each processing step in the defining diagram C Construct a Hierarchy Chart (shown on page 121)

37 Simple Program Design, Fourth Edition Chapter 8 37 Example 8.4 Process Three Characters Hierarchy Chart Process_three_characters Read_three_charactersSort_three_charactersPrint_three_characters Char_1, char_2, char_3

38 Simple Program Design, Fourth Edition Chapter 8 38 Example 8.4 Process Three Characters D Establish the Logic of the Solution Algorithm Using Pseudocode The mainline will call the module Read_three_characters, which will get the three input characters and send them to the mainline as parameters These parameters will then be passed to the module Sort_three_characters, which will sort the three characters and send these sorted values back to the mainline as parameters Refer to the code illustrated on page 122 of the textbook, which depicts the solution to the problem

39 Simple Program Design, Fourth Edition Chapter 8 39 Further Modularization The module Sort_three_characters in Example 8.4 contains some repeated code, which looks cumbersome Further modularization could be achieved by the introduction of a new module, called Swap_two_characters, which is called by the module Sort_three_characters Examine the code and hierarchy chart illustrated on pages 122 and 123 of the textbook

40 Simple Program Design, Fourth Edition Chapter 8 40 Further Modularization Hierarchy Chart Process_three_ characters Read_three_ characters Sort_three_ characters Print_three_ characters Swap_two_ characters

41 Simple Program Design, Fourth Edition Chapter 8 41 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 5.Develop the pseudocode for each successive module in the hierarchy chart 6.Desk check the solution algorithm

42 Simple Program Design, Fourth Edition Chapter 8 42 Programming Examples Using Modules Read and examine Examples 8.5 and 8.6, which depict the six steps in modularization

43 Simple Program Design, Fourth Edition Chapter 8 43 Summary This chapter introduced a modular approach to program design A module was defined as a section of an algorithm that is dedicated to the performance of a single function Top-down design was defined as the process of dividing a problem into major tasks and then into further subtasks within those major tasks until all the tasks have been identified

44 Simple Program Design, Fourth Edition Chapter 8 44 Summary Hierarchy charts were introduced as a method of illustrating the structure of a program that contains modules The steps in modularization that a programmer must follow were listed Programming examples using these six steps in modularization were then developed in pseudocode

45 Simple Program Design, Fourth Edition Chapter 8 45 END OF CHAPTER 8 Programming Problems 1) Design an algrorithm that will prompt for an accept an employee’s annual salary, and calculate the annual income tax due on that salary. Income is calculated according to the following table and is to be displayed on the screen. Portion of salaryIncome tax rate (%) $0 to $4999.990 $5000 to $9999.996 $10000 to $19999.9915 $20000 to $29999.9920 $30000 to $39999.9925 $40000 and above30

46 Simple Program Design, Fourth Edition Chapter 8 46 END OF CHAPTER 8 Programming Problems defining diagram: InputProcessingOutput salaryPrompt for salaryincome_tax Get salary Calculate income_tax Display income_tax

47 Simple Program Design, Fourth Edition Chapter 8 47 END OF CHAPTER 8 Programming Problems Hierarchy chart: : Alternative 1 Process_employee_salary Calculate_income_tax Salaryincome_tax

48 Simple Program Design, Fourth Edition Chapter 8 48 END OF CHAPTER 8 Programming Problems Solution algorithm : : Alternative 1 Process_employee_salary Prompt for salary Get salary DOWHILE salary NOT = 0 Calculate_income_tax (salary, income_tax) Display "Income tax due = ", income_tax Prompt for salary Get salary ENDDO END Calculate_income_tax (salary, income_tax) IF salary < 5,000 THEN income_tax = 0 ELSE IF salary < 10,000 THEN income_tax = salary * 0.06 ELSE IF salary < 20,000 THEN income_tax = salary * 0.15 ELSE IF salary < 30,000 THEN income_tax = salary * 0.2 ELSE IF salary < 40,000 THEN income_tax = salary * 0.25 ELSE income_tax = salary * 0.3 ENDIF END

49 Simple Program Design, Fourth Edition Chapter 8 49 Salary END OF CHAPTER 8 Programming Problems Hierarchy chart: : Alternative 2 Process_employee_salary Calculate_income_tax Salary income_tax Read_employee_salary

50 Simple Program Design, Fourth Edition Chapter 8 50 END OF CHAPTER 8 Programming Problems Solution algorithm : : Alternative 2 Process_employee_salary Read_employee_salary (salary) DOWHILE salary NOT = 0 Calculate_income_tax (salary, income_tax) Display "Income tax due = ", income_tax Read_employee_salary ENDDO END Calculate_income_tax (salary, income_tax) IF salary < 5,000 THEN income_tax = 0 ELSE IF salary < 10,000 THEN income_tax = salary * 0.06 ELSE IF salary < 20,000 THEN income_tax = salary * 0.15 ELSE IF salary < 30,000 THEN income_tax = salary * 0.2 ELSE IF salary < 40,000 THEN income_tax = salary * 0.25 ELSE income_tax = salary * 0.3 ENDIF END Read_employee_salary (salary) Prompt for salary Get salary END

51 Simple Program Design, Fourth Edition Chapter 8 51 END OF CHAPTER 8 Programming Problems 2) Design an algorithm that will prompt for and accept for numbers, sort them into ascending sequence and display them to the screen. Your algorithm is to include a module called Order_two_numbers that is to be called from the mainline to sort two numbers at a time. Defining diagram: InputProcessingOutput four numbers:Prompt for four numberssorted four numbers num1Get four numbers num2Sort numbers num3Display numbers num4

52 Simple Program Design, Fourth Edition Chapter 8 52 END OF CHAPTER 8 Programming Problems Sort_four_numbers Order_two_numbers number 1 number 2 Hierarchy chart:

53 Simple Program Design, Fourth Edition Chapter 8 53 END OF CHAPTER 8 Programming Problems solution algorithm: Sort_four_numbers Prompt for num1, num2, num3, num4 Get num1, num2, num3, num4 Set index to 1 DOWHILE index < 4 IF num1 > num2 THEN Order_two_numbers (num1, num2) ENDIF IF num2 > num3 THEN Order_two_numbers (num2, num3) ENDIF IF num3 > num4 THEN Order_two_numbers (num3, num4) ENDIF add 1 to index ENDDO Display num1, num2, num3, num4 END Order_two_numbers (number1, number2) temp = number1 number1 = number2 number2 = temp END


Download ppt "First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization."

Similar presentations


Ads by Google