Skip to content
Tactiverge

◇ Sensors · loops

Average a sensor

Add up a list of temperature readings with a loop and divide to get the mean.

  • Level 1 · Explorer
  • 5 tests
  • 🐞 debug variant
  • technically checked

Your task

A temperature sensor on a greenhouse controller reports one reading per minute. Before the controller decides whether to open a vent, it averages the last few readings so a single odd value does not trigger it.

Write average(readings) that returns the arithmetic mean of an array of numbers. If the array is empty, return 0 (there is nothing to average, and the controller must not crash).

Hints (0 of 3 shown)

    What gets checked

    • average([20, 22, 24]) → 22
    • average([1.5, 2.5]) → 2

    Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.