Lesson 64: Flowchart to sketch
From flowchart to code
- In the exam you trace flowcharts. Here you turn one into a sketch and run it.
- Each shape becomes a piece of code:
| Shape | Code |
|---|---|
| rectangle (process) | total = total + groupSize; |
| parallelogram (input) | read a value from the Serial Monitor |
| diamond (decision) | if (...) { ... } else { ... } |
| arrow back up | the next time round loop() |

finishedis the STOP box: once it is true,loop()does nothing more.
In the exam
- 0478/22/F/M/24 Q8: trace this flowchart with the data 7, 10, 2, 8, 3, 9, 0, 6. NumberGroups ends at 6, Total at 39, and the output is "Average group size 6". The 6 after the 0 is never input.
- 0478/22/F/M/23 Q8: the same flowchart for sale values, with Total / NumberSales. (a) With 5.50, 3.40, 6.25, 3.85, −11.00, 0 the output is "Average sale value 1.6". (b) Mark scheme: the error is that negative numbers are included; correct it with a decision box after the input that rejects negative numbers. Your second program does exactly this.
Dificuldade 3/5 · Moedas 200 + pequeno bônus na primeira conclusão hoje
Turn the exam flowchart on this page into code. Type each group size in the Serial Monitor. 0 is the rogue value: it means "no more groups". Then print Average group size and the average, worked out with whole-number division (DIV). After that the program does nothing more.
Clique em Executar para ver a saída aqui.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
The second exam flowchart works out the average sale value. With the data 5.50, 3.40, 6.25, 3.85, -11.00, 0 it prints 1.6: the error is that it also adds the negativo value. Fix it as in the mark scheme: after the input, add a decision that rejects a negative value. Print Rejected, and do not count or add it.
Clique em Executar para ver a saída aqui.