Example: Tracing an odd-number total through a flowchart
A flowchart shows an algorithm as connected symbols, with arrows giving the order of the steps. Pseudocode writes the same steps as structured, language-neutral text that reads like a program without following any one language's syntax. In this example you will move between the two forms and trace a loop by hand. The arrow ← means "store the value on the right in the variable on the left".
The flowchart below reads a positive whole number n and is meant to add the odd numbers from 1 up to n.
a) Name each type of symbol used, then write pseudocode that matches the flowchart exactly.
The rounded boxes at the top and bottom are terminators: they mark Start and End. The slanted boxes are ________ symbols, used for Input n and Output total. The rectangles are process symbols, each holding one assignment. The diamond is a ________ symbol with a Yes exit and a No exit.
The Yes path runs through two process boxes and then returns to a point above the diamond, so the condition is tested again before every pass. A loop whose condition is tested before each pass is a WHILE loop in pseudocode. The No exit leaves the loop and leads to the output.
- INPUT n
- total ← 0
- i ← 1
- WHILE i ≤ n
- total ← total + i
- i ← i + 2
- END WHILE
- OUTPUT total
The two indented lines are the loop body: they match the two boxes on the Yes path.
b) Trace the algorithm for the input n = 9. State the value that is output and how many times the condition i ≤ n is checked.
Before the loop, total = 0 and i = 1. Each pass checks i ≤ 9, adds i to total, then increases i by 2.
- Check 1: 1 ≤ 9 is true, so total becomes 0 + 1 = 1 and i becomes 3.
- Check 2: 3 ≤ 9 is true, so total becomes 1 + 3 = 4 and i becomes 5.
- Check 3: 5 ≤ 9 is true, so total becomes ________ and i becomes 7.
- Check 4: 7 ≤ 9 is true, so total becomes 9 + 7 = 16 and i becomes 9.
- Check 5: 9 ≤ 9 is true, so total becomes 16 + 9 = 25 and i becomes ________.
- Check 6: 11 ≤ 9 is false, so the No exit is followed.
The algorithm outputs 25, and the condition is checked 6 times: five true checks that run the loop body plus one final false check that ends the loop.
c) Change the flowchart so it adds the even numbers from 2 up to n. Which box changes, and what is output for n = 9?
The loop already steps by 2, so only the starting value needs to change: the box i ← 1 becomes i ← 2. In pseudocode, the third line becomes i ← 2, and nothing else changes.
Tracing for n = 9, i takes the values 2, 4, 6 and 8 while the condition is true, so the last even value added is ________. When i reaches ________, the check 10 ≤ 9 is false and the loop ends.
The total is 2 + 4 + 6 + 8 = 20, so the modified algorithm outputs 20.
A library checks a borrower's card and overdue status using the flowchart below. The borrower's card is active, and the borrower has no overdue books. Arrange the listed actions and checks in the order they are visited during this run.
1.
Display loan approved.
2.
Check whether books are overdue.
3.
Check whether the card is active.
4.
End.
5.
Start.
6.
Read the card status and overdue status.
A flowchart reads a test mark into the variable mark. A decision diamond asks mark ≥ 50? Its Yes exit leads to an output box that displays Pass, its No exit leads to an output box that displays Fail, and both paths then join and reach End. Which pseudocode matches this flowchart exactly?