Program Design (Copy)
1. Introduction to Program Design
- Program design is a crucial phase in software development.
- It involves breaking down a problem into structured components.
- Key tools used in program design include:
- Structure charts
- State-transition diagrams
- Pseudocode
2. Structure Charts
- A structure chart is a modeling tool used to decompose a problem into sub-tasks.
- It visually represents a program’s hierarchy, showing different modules and their interactions.
- Key Features of Structure Charts:
- Modules are represented by boxes.
- Arrows indicate the flow of parameters between modules.
- Each level refines the module above it.
- Example:
- Converting temperature from Fahrenheit to Celsius.
- Top-level module: Convert temperature.
- Sub-modules: Input temperature, Convert to Celsius, Output temperature.
- Selection in Structure Charts:
- Decision-making represented using a diamond-shaped box.
- Example: Converting between Fahrenheit and Celsius based on user input.
- Repetition in Structure Charts:
- Uses a semi-circular arrow to indicate loop conditions.
- Example: Repeating conversion until user enters a specific termination value (e.g., 999).
3. State-Transition Diagrams
- A Finite State Machine (FSM) is a model that represents a system in a specific state, transitioning between states based on inputs.
- Components of FSM:
- States: Represented as circles.
- Transitions: Indicated by arrows between states.
- Events: Labels on arrows specifying triggering conditions.
- Conditions: Written in square brackets next to an event label.
- Initial state: Marked with an arrow having a black dot.
- Final state: Indicated using a double circle.
- Use Cases of FSM in Program Design:
- Controlling user input validation.
- Managing multi-step processes (e.g., ATM operations, vending machines).
- Simulating real-world processes (e.g., traffic lights, game AI behavior).
4. Deriving Pseudocode from Structure Charts
- Step 1: Create an Identifier Table
- Lists variables, constants, and their descriptions.
- Example:
radius: Stores user input for radius.answer: Stores calculated value.pi: Constant set to 3.142.
- Step 2: Declare Constants and Variables in Pseudocode
DECLARE radius : REAL DECLARE answer : REAL CONSTANT pi ← 3.142 - Step 3: Define Functions for Calculations
FUNCTION calculateVolume(radius: REAL) RETURNS REAL RETURN (4 / 3) * pi * radius * radius * radius ENDFUNCTION FUNCTION calculateSurfaceArea(radius: REAL) RETURNS REAL RETURN 4 * pi * radius * radius ENDFUNCTION - Step 4: Define Procedures for Input and Output
PROCEDURE inputRadius OUTPUT "Please enter the radius of the sphere" INPUT radius WHILE radius < 0 DO OUTPUT "Please enter a positive number" INPUT radius ENDWHILE ENDPROCEDURE PROCEDURE outputAnswer OUTPUT answer ENDPROCEDURE - Step 5: Main Algorithm with Selection and Repetition
CALL inputRadius WHILE radius <> 0 OUTPUT "Do you want to calculate the Volume (V) or Surface Area (S)" INPUT reply IF reply = "V" THEN answer ← calculateVolume(radius) OUTPUT "Volume: ", answer ELSE answer ← calculateSurfaceArea(radius) OUTPUT "Surface Area: ", answer ENDIF CALL outputAnswer CALL inputRadius ENDWHILE
5. Activities for Practicing Program Design
- Activity 12A: Draw a structure chart to input height and width of a triangle, calculate and output its hypotenuse.
- Activity 12B: Modify the structure chart to calculate either the volume or surface area of a sphere.
- Activity 12C: Extend the algorithm to continue execution until a termination condition (e.g., entering 0) is met.
- Activity 12D: Implement a temperature conversion algorithm supporting both Fahrenheit to Celsius and Celsius to Fahrenheit conversions.
6. Summary of Key Concepts
- Structure Charts: Used for hierarchical decomposition of a program.
- State-Transition Diagrams: Used to document FSM behavior in programs.
- Pseudocode: Derived from structure charts for systematic program development.
- Design Principles: Essential for creating structured, modular, and maintainable code.
- Repetition and Selection: Incorporated into both structure charts and pseudocode.
