Lesson 64: Flowchart to sketch
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
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.
English
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.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
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.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
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 负形 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.
点击“运行”查看此处输出。