Write pseudocode from a flowchart
9.2 Algorithms – Writing Pseudocode from a Flowchart
What is a Flowchart?
A flowchart is a visual diagram that shows the steps of an algorithm. Think of it like a recipe card for a computer: each shape is a cooking step, and the arrows are the order in which you follow them. 🍰
Common Flowchart Symbols
| Symbol | Meaning | Pseudocode Equivalent |
|---|---|---|
| 🟢 (Oval) | Start or End | START / END |
| 🟠 (Rectangle) | Process / Action | ACTION |
| 🔵 (Diamond) | Decision / Branch | IF condition THEN … ELSE … END IF |
| ↔️ (Parallelogram) | Input / Output | READ / WRITE |
Translating Symbols to Pseudocode
- Identify the Start symbol and write
START. - For each Process rectangle, write the action in plain English, e.g.,
SET sum = sum + value. - When you hit a Decision diamond, translate it into an
IFstatement. Remember the two possible paths:THENandELSE. - Use
READfor input andWRITEfor output. - Finish with the End symbol and write
END.
Example: Sorting Numbers (Bubble Sort)
Let’s walk through a simple flowchart for Bubble Sort and write the pseudocode. Imagine you have a list of numbers that need to be sorted from smallest to largest.
- Start
- Read the list of numbers into array $A$.
- Set
n = length(A). - FOR
i = 1TOn-1DO - FOR
j = 1TOn-iDO - IF
A[j] > A[j+1]THEN - SWAP
A[j]andA[j+1] - END IF
- END FOR
- END FOR
- Write the sorted list.
- End
Practice Exercise
Below is a simple flowchart (described in words). Translate it into pseudocode. Use the symbols table as your guide.
- Start
- Read integer
n. - Set
sum = 0. - FOR
i = 1TOnDO - READ integer
x. - SET
sum = sum + x. - END FOR
- Write
sum. - End
Try writing it out, then compare with the answer below.
- START
- READ n
- SET sum = 0
- FOR i = 1 TO n DO
- READ x
- SET sum = sum + x
- END FOR
- WRITE sum
- END
Revision
Log in to practice.
2 views
0 suggestions