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

  1. Identify the Start symbol and write START.
  2. For each Process rectangle, write the action in plain English, e.g., SET sum = sum + value.
  3. When you hit a Decision diamond, translate it into an IF statement. Remember the two possible paths: THEN and ELSE.
  4. Use READ for input and WRITE for output.
  5. 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.

  1. Start
  2. Read the list of numbers into array $A$.
  3. Set n = length(A).
  4. FOR i = 1 TO n-1 DO
  5.  FOR j = 1 TO n-i DO
  6.   IF A[j] > A[j+1] THEN
  7.    SWAP A[j] and A[j+1]
  8.   END IF
  9.  END FOR
  10. END FOR
  11. Write the sorted list.
  12. 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 = 1 TO n DO
  •  READ integer x.
  •  SET sum = sum + x.
  • END FOR
  • Write sum.
  • End

Try writing it out, then compare with the answer below.

  1. START
  2. READ n
  3. SET sum = 0
  4. FOR i = 1 TO n DO
  5.  READ x
  6.  SET sum = sum + x
  7. END FOR
  8. WRITE sum
  9. END

Revision

Log in to practice.

2 views 0 suggestions