Back to Worked Examples
Algorithms Term 3

Flowchart to Pseudocode

Problem Statement

Convert the following flowchart into pseudocode. The flowchart calculates the total cost of items in a shopping cart, applying a 10% discount if the total exceeds $100.

Step-by-Step Solution

1

Study the flowchart

Identify each symbol and trace the flow from Start to End.

Start
INPUT: prices[]
total = 0
FOR EACH price IN prices[]
total = total + price
END FOR
total > 100?
Yes
total = total * 0.9
No
(skip)
OUTPUT: total
End
2

Identify the control structures

Look for sequences, selections (decisions), and iterations (loops).

  • Sequence: INPUT, set total = 0, OUTPUT (happens in order)
  • Iteration: FOR EACH loop to process all prices
  • Selection: IF total > 100 THEN apply discount
3

Write the pseudocode structure

Start with BEGIN/END and add each section in order.

BEGIN CalculateTotal
    // Input section

    // Initialize variables

    // Loop through items

    // Apply discount if applicable

    // Output result

END
4

Convert each symbol to pseudocode

Parallelogram (Input/Output):

INPUT: prices[] INPUT prices[]

Rectangle (Process):

total = 0 total = 0

Loop structure:

FOR EACH box FOR EACH price IN prices[]

Diamond (Decision):

total > 100? IF total > 100 THEN

Complete Pseudocode Solution

BEGIN CalculateTotal
    INPUT prices[]
    total = 0

    FOR EACH price IN prices[]
        total = total + price
    NEXT price

    IF total > 100 THEN
        total = total * 0.9
    ENDIF

    OUTPUT "Your total is: $" + total
END

Key Point: Notice how the pseudocode follows the exact same logic as the flowchart. Each symbol translates to one or more lines of pseudocode.

6

Verify with a trace table

Test with sample data: prices = [30, 50, 40]

Step price total total > 100?
Initialize - 0 -
Loop 1 30 30 -
Loop 2 50 80 -
Loop 3 40 120 -
Check - 120 TRUE
Discount - 108 -

Output: "Your total is: $108" (120 × 0.9 = 108)

Common Mistakes to Avoid

  • Forgetting to close loops (NEXT) and conditions (ENDIF)
  • Mixing up the Yes/No paths from a decision diamond
  • Not initializing variables before using them (total = 0)
  • Forgetting proper indentation to show structure