Lecture 2: Flowchart and Algorithm
Problem Solving
Problem-solving is the foundation of computer science.
A programmer must:
- Understand how humans solve problems.
- Translate this solution into a computer-executable algorithm.
- Implement it using a specific programming language syntax.
Computers may solve problems differently than humans, emphasizing efficiency and logic.
Stages of Problem Solving
- Analyze the problem.
- Write algorithm steps using:
- Pseudocode
- Flowchart
- Write the program in a programming language.
- Translate the program into machine code.
- Execute, test, and debug the program.
- Document the software.
Algorithms
Definition
An algorithm is a set of rules and procedures used to solve a problem in a finite number of steps.
It consists of ordered, unambiguous instructions that describe a process and can be implemented in any programming language.
Characteristics of a Good Algorithm
- Each step must be clear and unambiguous.
- The algorithm must terminate after a finite number of steps.
- The process must lead to the correct solution.
- Steps must be written in precise language.
Algorithm Design Techniques
Algorithms can be expressed using:
1. Flowcharts
A graphical representation that shows the logical sequence of operations and their relationships.
2. Pseudocode
A textual representation that uses English-like statements to describe program logic.
Pseudocode
Definition
Pseudocode is a simplified, language-like representation of algorithmic logic written using commands that resemble real programming languages.
It focuses on problem-solving logic rather than language syntax and can be easily converted into actual code.
Components
- Keywords
- Sections
- Statements
Structure Types
- Sequential Statements: Executed one after another.
- Decision Statements: Use
if-elseorswitch-casefor conditional execution. - Looping Statements: Use
for,while, ordo-whilefor repetition until a condition is met.
Flowcharts
Definition
A flowchart is a graphical representation showing the sequence of steps in a process or program, using standardized symbols.
It is one of the most effective tools for analyzing and visualizing systems.
Importance and Uses of Flowcharts
Simplify Understanding
- Show logical flow of processes visually.
- Easy for programmers and non-programmers to grasp.
Assist in System Analysis
- Identify the relationship between input, process, and output.
- Detect errors or inefficiencies early.
Aid in Program Development
- Each flowchart step can be directly translated into code (e.g., in C++).
- Minimizes logical errors before implementation.
Documentation and Maintenance
- Acts as a reference document for future updates and debugging.
Enhance Communication
- Provides a common visual language between developers and analysts.
Detect Logic Errors Early
- Logic can be tested visually before coding.
Common Flowchart Symbols
| Symbol | Shape | Description |
|---|---|---|
| Terminator | Oval | Represents the Start or End of the process. |
| Flow Line | Arrow | Shows the direction of control flow. |
| Process | Rectangle | Denotes an operation or calculation. |
| Decision | Diamond | Represents a conditional check (Yes/No or True/False). |
| Input/Output | Parallelogram | Indicates data input or output operations. |
| Connector | Circle | Connects different parts of a flowchart. |
| Loop Symbol | Hexagon | Represents a loop structure (e.g., for-loop). |
| Text Label | — | Describes the function or operation in the shape. |
Flowchart Design Guidelines
- The flowchart must be neat, complete, easy to follow, and unambiguous.
- Flow direction should be left-to-right and top-to-bottom.
- Start symbol: one outgoing flow line only.
- End symbol: one incoming flow line only.
- Process symbol: one outgoing flow line.
- Decision symbol: one incoming and two outgoing flow lines.
- Use annotations to clarify logic when needed.
Key Qualities of a Good Flowchart
- Neat: Well-organized with no unnecessary line crossings.
- Complete: Includes all essential steps from start to finish.
- Easy to Follow: Logical and clearly directed flow.
- Unambiguous: Each symbol and statement must have a single, clear meaning.
A good flowchart should be clear, logical, and universally understandable.
Advantages of Flowcharts
- Simplify communication and understanding.
- Allow for effective problem analysis.
- Serve as proper documentation tools.
- Help write programs more efficiently.
- Facilitate error detection and program correction.
Disadvantages of Flowcharts
- Can become complex for large problems.
- Modifications often require redrawing the entire flowchart.
- Manual reproduction of flowcharts can be cumbersome.
- Excessive details may distract from the main logic.
Summary
- Algorithms provide structured solutions to problems.
- Pseudocode expresses logic in human-readable form.
- Flowcharts visualize processes using standardized symbols.
- Good flowcharts and pseudocode make programming more reliable, maintainable, and efficient.