T&T
INFO 0102
INTRODUCTION TO PROBLEM SOLVING
Modular Programming
Agenda
Definitions
Modularization
The Modularization Process
The Mainline
Benefits of Modular Programming
Example
Modularizing Programs in C
Definitions
Procedural Programming
Definition: Procedural programming is based on a structured, top-down approach to writing effective programs. The approach concentrates on what a program has to do and involves identifying and organizing the processes in the program solution.
The problem is usually broken down into separate tasks or functions and includes top-down development and modular design.
Top-Down Development
In the top-down development of a program design, a general solution to the problem is outlined first. This outline is then divided gradually into more detailed steps until finally, the most detailed levels have been completed.
It is only after this process of top-down development (also called functional decomposition or stepwise refinement) that the programmer starts to code.
Outcome: The result of this systematic, disciplined approach to program design is a higher precision of programming than was previously possible.
Modular Design
Definition and Purpose
Procedural programming also incorporates the concept of modular design, which involves grouping tasks together because they all perform the same function (e.g., calculating sales tax or printing report headings).
Modular design is directly connected to top-down development, as the steps or subtasks into which the program solution is divided actually form the future modules of the program.
Benefit: Good modular design also assists in the reading and understanding of the program.
Modularization
Definition of Modularization
As the complexity of programming problems increases, it becomes more challenging to consider the solution as a whole. When faced with a complex problem, one needs to divide the problem into smaller parts.
Process: First, identify the major tasks to be performed, and then divide the problem into sections representing those tasks. These sections can be referred to as subtasks or functions.
Once the major tasks have been identified, further subtasks within them are identified, leading to a more detailed breakdown. This process is known as top-down design (or functional decomposition or stepwise refinement).
Characteristics of Modularization
A module can be defined as a section of an algorithm that is dedicated to a single function.
Benefits of Modules:
Simplifies the algorithm.
Makes it more systematic.
Reduces the likelihood of errors.
Since each module represents a single task, solutions can be developed task-by-task until a complete solution is devised.
Modularization is the process of dividing a problem into separate tasks, each with a single purpose. Top-down design allows you to concentrate on the overall design of the algorithm before handling the details of lower-level modules.
Summary of Top-Down Design
Analogy: Top-down design is akin to planning a big project by breaking it into smaller, manageable pieces. Start with the big picture and divide it into smaller tasks (modules).
Key Points:
A module performs a specific job.
By splitting the program into modules, it becomes easier to understand, build, and fix.
This modular approach reduces confusion and the chance of errors.
Formula for Top-Down Design: Start big D break into small pieces D solve each piece separately then integrate.
The Modularization Process
Steps in Modularizing
Definition: Write down the activities or processing steps to be performed.
Grouping: These activities are grouped together into manageable tasks (functions) that ultimately lead to modules.
Focus: When defining the problem, the emphasis must remain on the tasks or functions required.
Size of Modules: Each module must be large enough to perform its task, including only those operations that contribute to that task.
Single Entry/Exit: A module should have a single entry and a single exit, following a top-to-bottom sequence of instructions.
Naming Conventions
The name of the module should describe the work as a specific function. Using a verb followed by a two-word object aids in identifying the task.
Examples of Typical Module Names:
Print_page_headingsCalculate_sales_taxValidate_input_date
The Mainline
Purpose of the Mainline
Each module performs a specific task, and a mainline routine must coordinate these modules.
Characteristics of the mainline:
Provides master control over the modules.
Lists the main processing functions and their order.
Should be easy to read, manageable in length, and demonstrate sound logic structure.
Analogy for Understanding
Think of a program like a play or movie:
Each module acts as an actor (performing a specific task).
The mainline routine acts like a director (coordinating actions and order).
Traits of the Mainline
Controls and coordinates all the modules.
Highlights the major steps of the program.
Should be easily understandable by reading pseudocode, focusing on logic rather than details.
Summary Statement: Modules execute tasks, while the mainline directs their execution order.
Benefits of Modular Programming
Key Advantages
Ease of Understanding: Each module should perform one function, making the program easier to comprehend.
Reusable Code: Modules from one program can be reused in other programs.
Elimination of Redundancy: Aids in avoiding the repetition of writing the same segment of code multiple times.
Efficiency of Maintenance: As each module is self-contained, changes in one should have little or no impact on others.
Example of Modularization
Defining Diagram Task
Objective: Design an algorithm to prompt for three characters, accept the input, sort them into ascending order, and output the result, continuing until 'XXX' is received.
Steps to Develop the Algorithm
Defining Diagram Overview
Input:
char_1: Prompt for characterschar_2: Accept three characterschar_3: Sort three charactersOutput:
Output three characters
Original Solution Algorithm
Steps in Algorithm
Process three characters
Prompt for
char_1,char_2,char_3Get
char_1,char_2,char_3DOWHILE NOT (char_1 = 'X' AND char_2 = 'X' AND char_3 = 'X')
If
char_1 > char_2thentemp = char_1char_1 = char_2char_2 = temp
If
char_2 > char_3thentemp = char_2char_2 = char_3char_3 = temp
If
char_1 > char_2thentemp = char_1char_1 = char_2char_2 = temp
Output
char_1,char_2,char_3Prompt for
char_1,char_2,char_3Get
char_1,char_2,char_3ENDDO
Enhanced Solution Algorithm
Benefits of Modularization
The initial solution is cumbersome with repetitive code. Thus, it’s ideal for modularisation.
Introduce a module named
Sort_three_charactersthat encapsulates the sorting logic.The updated algorithm consists of two major modules:
Mainline module:
Process_three_charactersSubmodule:
Sort_three_characters
Control Flow in Modular Architecture
The mainline calls the sorting module when needed, enhancing readability and maintainability.
This modular setup allows clearer representation, potentially visualized through a hierarchy chart.
Modularizing Programs in C
Functions in C
In C, functions are utilized to modularize programs. The typical approach combines new functions written by the programmer with prepackaged functions from the C standard library.
Key Functions Provided by the C Standard Library:
A rich array of functions for mathematical calculations, string manipulation, character manipulation, input/output, and more.
Programmer-Defined Functions:
Allow creating specific functionality tailored to a program's requirements.
Defined uniquely and hidden from other functions until called.
Function Invocation
Functions are invoked through a function call that specifies the function name and provides arguments necessary for operation.
Analogy: Similar to a hierarchical management process where a boss (caller) assigns a task to a worker (called function) who reports back upon completion.
Predefined Libraries vs User-Defined Libraries
Predefined (Library) Functions
Overview
Built-in functions part of the C standard library that are ready for use, pre-tested, and optimized by developers.
Definition:
Already coded and stored in C libraries.
Purpose:
Perform common tasks such as input/output operations, mathematical calculations, and string manipulation.
Examples:
printf()andscanf()(input/output - instdio.h)sqrt()andpow()(mathematical functions - inmath.h)strlen()andstrcpy()(string operations - instring.h)
User-Defined Functions
Overview
Functions created by the programmer for specific tasks suited to the program’s needs.
Definition:
Coded by the programmer within the source code.
Purpose:
Resolve specific requirements, enhance modularity by segmenting tasks logically, avoid redundancy.
Implementation Steps:
Declaration (Prototype): Informs the compiler of the function's name, return type, and parameters prior to its use. Typically placed before the
main()function or in a separate header file.Definition: Contains the actual set of statements executed when the function is called.
Call (Invocation): Executes the function from another part of the program, such as the
main()function, using its name and the requisite arguments.
Practice Questions
State briefly the three control structures used in programming and problem solving.
Identify suitable data types for the following data:
i. `