Motivation Complexity of programming problem more difficult to consider the solution as a whole clue: dividing the problem into small parts (module) Each module represent single task, free of error Modularisation : is the process of dividing a problem into separate tasks, each with a single purpose.
Benefits of modular design Ease of understanding each module has only one function Reusable code Elimination of redundancy avoid the repetition of writing Efficiency of maintenance
Modular name convention Describe the work to be done as a single specific function Use a verb, followed by a two-word object. Example: –Print_page_heading –Calculate_sales_tax –Validate_input_date
Example 8.1 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.
Defining diagram InputProcessingOutput Char_1 Char_2 Char_3 Prompt for characters Accept three characters Sort three characters Ouput three characters Char_1 Char_2 Char_3
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`AND 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_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 operator for char_1, char_2, char_3 Get char_1, char_2, char_3 ENDDO END
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`AND char_2=´X´AND char_3=´X´) Sort_three_characters Output to the screen char_1,char_2,char_3 Prompt operator for char_1, char_2, char_3 Get char_1,char_2,char_3 ENDDO END Sort_three_characters 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 ´Controlling module´or ´Calling module´or ´Mainline“ `Subordinate module` or `Called Module`
Hierarchy Chart Example 8.1 Process_three_ characters Sort_three_ characters Calling module Called module
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.
Defining diagram InputProcessingOutput Char_1 Char_2 Char_3 Prompt for characters Accept three characters Sort three characters Ouput three characters Char_1 Char_2 Char_3
Solution Algorithm Process_three_characters Read_three_characters DOWHILE NOT (char_1=´X`AND char_2=´X´AND char_3=´X´) Sort_three_characters Print_three_characters Read_three_characters ENDDO END Read_three_characters Prompt the operator for char_1,char_2,char_3 Get 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_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 Print_three_characters Output to the screen char_1, char_2, char_3 END Mainline 1rst Module 2nd Module 3rd Module
Hierarchy charts Process_three_ characters Read_three_ characters Sort_three_ characters Print_three_ characters