Same readings as last lesson, now we compute with them: sum, average, minimum, maximum and counts. This is exactly what lab experiments 10 and 17 ask for.
Quick recall question: "What is the last valid index of readings[5]?" (4)
Every scene in its final state, with the teacher's notes. Made for review on a phone.
Same readings as last lesson, now we compute with them: sum, average, minimum, maximum and counts. This is exactly what lab experiments 10 and 17 ask for.
Quick recall question: "What is the last valid index of readings[5]?" (4)
The field mission: a production test rig loses track of which reading belongs to which channel. Four stages, one per click: acquire, store, analyze, report.
Every program today fits this shape. The Ministry lab exam draws experiments 10 and 17 from this unit.
One click per iteration. Before each click, students say the new total aloud: 4, 11, 13, 18. The trace table on the right is what they write in their notebooks.
Ask: why does sum start at 0? (Adding 0 changes nothing.) Why must each element be added exactly once? (Skip one and the sum is wrong; add one twice and it is wrong too.)
Wild moment: four students hold the cards 4, 7, 2, 5. A fifth student is "sum" with a mini whiteboard. The class is the CPU: they call out each addition, "sum" writes the new value and wipes the old.
Tom's test: sum 55, length 10. Without the cast, 55 / 10 is integer division and gives 5, then 5 is stored in the double as 5.000. With the cast, the division is done in double: 5.500.
Lab experiment 17 asks for the average with three decimals: %.3f. Precondition: LENGTH is greater than 0.
Both extremes start from readings[0], real data. The loop then compares the remaining elements, so it starts at i = 1.
Link to the loops unit: "first valid value" initialization. Avoid invented values such as minimum = 0: with all-positive readings the answer would be 0, which is not even in the data. This works for positive, zero and negative data. Precondition: LENGTH > 0.
One click per iteration. A box changes only when the current element proves a new extreme: 398 is a new minimum, 421 a new maximum, 405 and 399 change nothing.
Hide the last two rows (cover the screen edge) and let students fill them in before you click.
Count the readings at or above 410. A reading of exactly 410 would count (the boundary). Expected result with Tom's data: 2.
Ask: why can't i and high_count be the same variable? i counts positions visited (always 5 at the end); high_count counts the values that match.
Interactive: click a target and the loop runs again. The data is Tom's third acceptance test. -5 occurs 3 times, 0 four times, 5 three times, 7 never.
Predict first by show of hands, then click. Four tests to remember: absent, once, repeated, all equal.
The change between neighbouring sensor samples. The loop starts at 1 because index 0 has no previous element: i - 1 would be -1.
The same pattern checks whether an array is sorted (each value at least the previous one): practice question 24.
Look for 21. found starts at -1, which means "not found" because -1 can never be a valid index. At index 3 the values match, found becomes 3 and break leaves the loop.
Ask: when is break correct? Only when the task asks for the first match. For the last match, keep going.
Connect to unit 7. The function gets the array and its length. One click per line of the contract, then the call: the sum of the five readings is 2035.
Pointer details come later. For now, the rule: always pass the length with the array.
One left-to-right pass over 7 3 6 2. Each click compares one neighbouring pair and swaps it if it is out of order. After the pass, 7 is at the end.
Wild moment: four students with the cards stand in a row and swap places when the class shouts "swap". Then ask: how many passes until the whole row is sorted? Full sorting is unit 10.
Bug hunt race: pairs get 2 minutes to find all four faults on paper. Then one click per fault. Do not accept a repair without the test: 1 2 3 4 5 must give 15.
Students mark each faulty token before they edit anything.
Tom's acceptance tests for the mission. Each test targets a different likely bug: the mixed test checks the normal path, the all-equal test checks the starting values and tie counting, the negative test catches code that assumes positive data.
Exact labels may vary; the numbers and the three-decimal average must match.
Students answer on paper first: valid indexes, the final count, what data[4] means, and a loop that prints the array backward. Then the click reveals the answers.
Read the words aloud in English and Hebrew.
Team mission, about 40 minutes. Recovery: separate input, sum, extremes and count loops. Stretch: report the first index of each extreme.
Rubric: safe bounds 5, correct statistics 7, occurrence counts 4, test evidence 3, readable code 1.
Homework: practice page, questions 6 to 10, 13, 16 to 19, 28 and 29.