Week 4 Notes: Algorithms, Pseudocode, and Flowchart
Algorithms
- Definition: A set of step-by-step instructions to accomplish a task.
- Must have a start instruction and a stop instruction.
- Each instruction must be:
- precise
- unambiguous
- executable in finite time
- Origin of the term:
- The word algorithm derives from the phonetic pronunciation of the name Abu Ja'far Mohammed ibn Musa al-Khwarizmi, an Arabic mathematician who developed a set of rules for the four basic arithmetic operations.
- A procedure is a finite sequence of well-defined instructions that can be mechanically carried out in a finite amount of time.
- Relationship to problem solving:
- Algorithms describe the method; pseudocode/flowcharts help design and communicate the method before coding.
Basic Algorithm Concept
- Input List: A list of inputs required to solve the problem.
- Output List: The results produced after processing the inputs.
- A step-by-step method for solving a problem or doing a task.
Examples
Sum of two numbers
- Steps:
- Start
- Declare variables num1, num2, sum
- Read values num1 and num2
- Compute sum = num1 + num2
- Display sum
- Stop
- Input: two numbers (num1, num2)
- Process: sum = num1 + num2
- Output: display the sum (sum)
- Formula:
Area of a rectangle
- Steps:
- Start
- Declare variables length, width, areaR
- Read values length and width
- Compute areaR = length × width
- Display areaR
- Stop
- Input: length and width
- Process: areaR = length × width
- Output: display the area (areaR)
- Formula:
- Note: Some slides show areaR = length + width in the process line due to a typo; the correct operation is multiplication.
Largest of two numbers
- Steps:
- Start
- Declare variables val1, val2, ans
- Read values val1 and val2
- If val1 > val2 then print that val1 is largest
- Else print that val2 is largest
- Stop
- Process: if (val1 > val2) then print that val1 is largest; else print that val2 is largest
- Output: display the answer (ans)
Flowchart
- Flowchart definition: A tool to show the steps involved in a process.
- It is a diagram made up of boxes, diamonds and other shapes, connected by arrows. Each shape represents a step and arrows show the order.
- It depicts the flow or sequence of a program or process.
Flowcharting Basic Symbols
- Start/End symbol: Terminator; marks the starting or ending point; usually contains Start or End.
- Action or Process symbol: A box representing a single step or a sub-process.
- Input/Output symbol: Represents data entering or leaving the system (input or output).
- Decision symbol: A diamond indicating a branching point; multiple lines for different decisions.
- Preparation symbol: Sets up to another step.
- Flow Lines: Arrows showing direction of flow.
Additional Symbols (as listed on slides)
- Terminal
- Process
- Condition
- Data
- Stored Data
- Connector
- Document
- Delay
- Display
- Preparation
- Manual Input
- Multidocument
- Database
- Flow Line
- Manual Operation
- Predefined Process
- Card
- Paper Tape
- Direct Data
General Rules for Flowcharting
- All boxes are connected with arrows (flow lines).
- Flowchart symbols have an entry point on the top of the symbol; exit point is on the bottom for most symbols; the Decision symbol has two exit points (sides or bottom).
- Generally, flow proceeds from top to bottom; upward flow is allowed only up to 3 symbols.
- Connectors are used to bridge breaks in the flowchart.
- Subroutines and interrupt programs have their own independent flowcharts.
- All flowcharts start with a Terminal or Predefined Process symbol (for interrupt programs or subroutines).
- All flowcharts end with a Terminal symbol.
Algorithm/Flowchart to Find the Sum of Two Numbers
- Steps (same as earlier): Start → Declare num1, num2, sum → Read num1, num2 → Compute sum → Print sum → Stop
- Pseudocode (equivalent):
- START
- Declare num1, num2, sum
- sum = num1 + num2
- PRINT sum
- STOP
Algorithm to Find the Largest of Two Numbers (Pseudocode/Flowchart)
- Steps (same as earlier): Start → Declare val1, val2, ans → Read val1, val2 → If val1 > val2 then print val1 is largest else print val2 is largest → Stop
- Pseudocode (equivalent):
- START
- DECLARE Val1, Val2, Ans
- READ Val1, Val2
- IF Val1 > Val2 THEN PRINT Val1 is largest
- ELSE PRINT Val2 is largest
- ENDIF
- STOP
Pseudocode
- Definition: An artificial and informal language that helps develop algorithms.
- Purpose: Useful for developing algorithms that will later be translated into a programming language (e.g., C).
- Characteristic: Similar to English statements; not executed by a computer.
Pseudocode Constructs
- SEQUENCE: linear tasks performed one after another.
- WHILE: a loop with a condition at its beginning.
- REPEAT-UNTIL: a loop with a condition at the bottom.
- FOR: another looping construct.
- IF-THEN-ELSE: conditional branching.
- CASE: generalization of IF-THEN-ELSE (switch-like).
- These constructs are keywords used to describe the control flow of the algorithm.
Pseudocode Constructs: Keywords and Mappings
- SEQUENCE: Input: READ, OBTAIN, GET; Output: PRINT, DISPLAY, SHOW; Compute: COMPUTE, CALCULATE, DETERMINE; Initialize: SET, INIT; Add: INCREMENT, BUMP; Sub: DECREMENT
- FOR: for iteration bounds
- WHILE: while condition, sequence, ENDWHILE
- CASE: CASE expression OF … ENDCASE
- REPEAT-UNTIL: REPEAT, UNTIL condition
- IF-THEN-ELSE: IF condition THEN … ELSE … ENDIF
Example: Area of a Rectangle (Pseudocode variant)
- Steps: Start → Declare length, width, areaR → Read length, width → Compute areaR = length × width → Print areaR → Stop
- Alternative starting code (as shown on slides):
- Start
- Set length = 0, width = 0, areaR = 0
- Read length, width
- Compute areaR = length × width
- Print areaR
- Stop
Example: Largest of Two Numbers (Pseudocode variant)
- Steps: Start → Declare val1, val2, ans → Read val1, val2 → If val1 > val2 then print val1 is largest else print val2 is largest → Stop
- Alternative starting code (as shown on slides):
- Start
- INIT val1 = 0, val2 = 0, ans = 0
- GET val1, val2
- IF Val1 > Val2 THEN PRINT val1 is largest
- ELSE PRINT val2 is largest
- ENDIF
- Stop
Basic Structure of a C Program
- Example shown:
- #include
- int main() { // your code
- printf("Hello World!");
- return 0;
- }
- #include
- Notes:
- #include
is a header file that lets us work with input and output functions, such as printf(). - Main function is the entry point of the program; code inside {} is executed.
- Every C statement ends with a semicolon ;
- printf() is a function used to output text to the screen.
- return 0; ends the main function.
- Header files add functionality to C programs.
- #include
Practical Connections
- Flowcharts and pseudocode are tools to design, communicate, and debug algorithms before coding.
- They help ensure clarity, reduce implementation errors, and facilitate collaboration between team members.
- In coursework, these concepts bridge from problem analysis to actual programming (C, etc.).
Real-World Relevance and Implications
- Correctness and unambiguity in algorithms are crucial for predictable software behavior.
- Flowcharts aid in documentation and maintenance, especially for complex business processes or legacy systems.
- Pseudocode acts as a transitional artifact between design and implementation across programming languages.
- Ethical/practical implications: clear communication of logic, avoiding ambiguous symbols, and ensuring accessibility for teams with varying expertise.
Quick Reference: Key Formulas and Rules
- Sum of two numbers:
- Area of a rectangle:
- Flow direction: generally top-to-bottom; upward flows allowed up to 3 symbols.
- Common pseudocode keywords:
- Input: READ, OBTAIN, GET
- Output: PRINT, DISPLAY, SHOW
- Compute: COMPUTE, CALCULATE, DETERMINE
- Initialize: SET, INIT
- Looping: FOR, WHILE, REPEAT-UNTIL
- Conditional: IF-THEN-ELSE, CASE
- Example program structure for C:
- Preprocessor directive: #include
- Entry point: int main() { … }
- Output: printf("…");
- Termination: return 0;
- Preprocessor directive: #include
Summary
- Algorithms define the method; flowcharts and pseudocode help design and visualize the method.
- Simple problems (sum, area, max) illustrate how to translate a task into steps, a flowchart, and pseudocode.
- C program basics connect the algorithmic design to actual implementation.