Isp Lesson 4 Share (1)
Isp Lesson 4 Share (1)
STRUCTURE
PROGRAMMING
NKANNAN
Principles of Good Program Design
• KISS (Keep It Simple, Stupid)
• Simplicity is key. Avoid unnecessary complexity to make the program
easier to understand and maintain.
• DRY (Don’t Repeat Yourself)
• Reuse code whenever possible to avoid duplication and reduce the risk of
inconsistencies.
• YAGNI (You Aren’t Gonna Need It)
• Focus on current requirements rather than adding features that might be
needed in the future.
Principles of Good Program Design
• Separation of Concerns
• Divide the program into distinct sections, each addressing a specific
concern (e.g., UI, business logic, data storage).
• Scalability and Performance
• Design the program to handle growth in data or user load without
significant rework.
• Maintainability
• Write clean, well-documented code that can be easily updated or
extended by other developers.
Tools and Techniques for Program Design
• Flowcharts and Diagrams: Visual tools like UML (Unified Modeling
Language) diagrams help represent the program’s structure and flow.
• Pseudocode: A high-level description of the program’s logic that
bridges the gap between design and implementation.
• Prototyping: Creating a simplified version of the program to test ideas
and gather feedback.
• Version Control Systems: Tools like Git help manage changes and
collaborate with others during the design and development process.
Characteristics of Programming Languages
1. Readability: How easily the code can be understood by humans. High-level languages prioritize
readability.
2. Writability: How easily programs can be written in the language.
3. Portability: The ability of code to run on different platforms or systems without modification.
4. Efficiency: How well the language utilizes system resources (e.g., memory, processing power).
5. Abstraction: The level of detail hidden from the programmer. High-level languages provide more
abstraction.
6. Expressiveness: The ability to express complex operations concisely.
7. Support for Paradigms: Whether the language supports multiple programming paradigms (e.g.,
procedural, object-oriented, functional).
Properties of Programming Languages
1. Syntax: The set of rules that define the structure of valid code (e.g., how to write loops,
conditionals, and functions).
2. Semantics: The meaning of the code and how it behaves when executed.
3. Type System: Defines how data types are handled (e.g., static vs. dynamic typing, strong vs.
weak typing).
4. Memory Management: How the language handles memory allocation and deallocation (e.g.,
manual in C, automatic in Python).
5. Concurrency Support: The ability to handle multiple tasks simultaneously.
6. Standard Library: The collection of pre-written code and functions provided by the language.
7. Error Handling: Mechanisms for detecting and managing errors (e.g., exceptions, assertions).
ALGORITHM
ALGORITHM DESIGN
• An algorithm is a step-by-step procedure, or a set of rules designed to
solve a specific problem or accomplish a task. It serves as the
foundation for program design, offering a clear path from input to
output. Algorithms are independent of programming languages,
meaning they can be implemented in any language once the logic is
defined. For example, an algorithm to find the largest number in a list
involves steps like initializing a maximum variable, iterating through
the list, and updating the maximum variable when a larger number is
found.
ALGORITHM DESIGN - Key characteristics of
an effective algorithm
• Clarity: Each step should be precise and unambiguous.
• Finiteness: The algorithm must terminate after a finite number of
steps.
• Efficiency: The solution should minimize computational resources
like time and memory.
ALGORITHM DESIGN - Writing Clear and
Efficient Algorithms
Writing clear and efficient algorithms involves:
1. Understanding the Problem: Clearly define the input, output, and the
constraints.
2. Breaking Down the Problem: Divide the task into smaller, manageable
components.
3. Defining Steps Clearly: Use simple, logical statements to describe each action.
4. Optimizing Performance: Reduce redundancy and improve time complexity by
using efficient techniques like sorting or hashing.
5. Testing: Validate the algorithm with different test cases, including edge cases.
ALGORITHM DESIGN
• Example: To find the sum of integers from 1 to n:
1. Initialize sum = 0
2. For each integer i from 1 to n, add i to sum
3. Return sum
ALGORITHM DESIGN - Algorithm representation
using pseudo-code and flowcharts:
• Both pseudo-code and flowcharts are tools used to represent
algorithms in a clear and understandable way. They serve as
intermediate steps between the problem-solving phase and the actual
implementation of the algorithm in a programming language. Below is a
detailed explanation of each concept and how they are used to represent
algorithms.
Pseudo-code
• Pseudo-code is a high-level, human-readable description of an
algorithm. It uses a mixture of natural language and programming-like
syntax to outline the steps of an algorithm without worrying about the
strict rules of a specific programming language.
Pseudo-code
Key Features of Pseudo-code:
• Language-independent: It is not tied to any specific programming
language, making it accessible to a wide audience.
• Informal syntax: It uses simple, readable structures like loops,
conditionals, and functions.
• Focus on logic: It emphasizes the logic and flow of the algorithm rather
than implementation details.
• No strict rules: There is no formal syntax for pseudo-code, so it can vary
depending on the writer's preference.
Pseudo-code
How Pseudo-code Represents Algorithms:
• It breaks down the algorithm into smaller, manageable steps.
• It uses constructs like IF-ELSE, FOR, WHILE, and FUNCTION to
represent decision-making, repetition, and modularity.
• It avoids low-level details like variable declarations or memory
management.
• BEGIN
Pseudo-code • INPUT number
Example of Pseudo-code:
• IF number > 0 THEN
• PRINT "Positive"
• ELSE IF number < 0 THEN
• PRINT "Negative"
• ELSE
This pseudo-code
• PRINT "Zero"
describes an algorithm to
• END IF
determine if a number is
positive, negative, or zero. • END
Pseudo-code
Advantages of Pseudo-code:
• Easy to write and understand.
• Helps in planning and debugging before coding.
• Can be translated into any programming language.
Flowcharts
• A flowchart is a graphical representation of an algorithm. It uses
symbols and arrows to depict the flow of steps, decisions, and processes
in an algorithm. Flowcharts are particularly useful for visualizing the
logic and structure of an algorithm.
Key Components of a Flowchart:
• Start/End Symbol (Oval): Represents the beginning or end of the
algorithm
Key Components of a Flowchart:
• Process Symbol (Rectangle): Represents an action or operation (e.g.,
calculations, assignments)
Key Components of a Flowchart:
• Decision Symbol (Diamond): Represents a conditional statement
(e.g., IF-ELSE).
Key Components of a Flowchart:
• Input/Output Symbol (Parallelogram): Represents input or output
operations (e.g., reading input, displaying output)
Key Components of a Flowchart:
• Arrow (Flowline): Shows the direction of the algorithm's flow.
Key Components of a Flowchart:
• Connector (Circle): Used to connect different parts of the flowchart,
especially in large or complex diagrams
Flowcharts
How Flowcharts Represent Algorithms:
• They visually map out the sequence of steps in an algorithm.
• They highlight decision points and loops, making it easier to
understand the logic.
• They are particularly useful for complex algorithms with multiple
branches or iterations.
Flowcharts
Example of a Flowchart:
• A flowchart for the same algorithm (checking if a number is positive,
negative, or zero) would look like this:
START
Input number
Is number > 0?
T Print
"Positive"
F
T Print
Is number < 0?
"Negative"
F
Print "Zero"
END
Flowcharts
Advantages of Flowcharts:
• Provides a visual representation of the algorithm, making it easier to
understand.
• Helps identify errors or inefficiencies in the logic.
• Useful for communicating algorithms to non-programmers.
Comparison of Pseudo-code and Flowcharts
Feature Pseudo-code Flowcharts
Representation Text-based Graphical
Ease of Use Easy to write and modify Requires drawing tools or
software
Focus Emphasizes logic and Emphasizes flow and
structure decision points
Audience Programmers and Both technical and non-
developers technical
Scalability Suitable for complex Can become cluttered for
algorithms large algorithms
Flowcharts
How They Are Used Together
• Pseudo-code is often used to draft the initial logic of an algorithm in
a textual format.
• Flowcharts are then used to visualize the same logic, especially for
complex or branching algorithms.
• Together, they provide a comprehensive understanding of the
algorithm before it is implemented in code.
Flowcharts
• By using both pseudo-code and flowcharts, developers can ensure that
the algorithm is well-designed, logically sound, and easy to implement
in any programming language.