BlyxDocumentation
Browse chapters
Documentation / Practical projects

Capstone project — build a sensor report

Build a complete report from a blank file, handle empty input, and verify a boundary condition.

Specify the report

Given a vector of readings and a limit, print their total and how many exceed the limit. For an empty batch, print no readings and stop. Before coding, write down the expected behavior for an empty batch, one reading equal to the limit, and a batch with readings on both sides.

Build the counting function

Start with fn hot_count(readings: vector, limit: f64) -> i64. Start hits and i at zero. While i is less than len(readings), inspect at(readings, i). Increment hits only for a reading strictly above the limit. Advance i on every pass and return hits after the loop.

Build the report function

Take the same two parameters. Check for an empty batch first. The early return lets the normal report follow at the same indentation. Use sum for the total, call hot_count for the count, and pair print! labels with println! values.

Trace and verify

The four readings total 123.5. Only 31.5 and 34.0 exceed the limit of 30.0. The empty batch exercises a separate branch without reading an element. Verify the complete program in both the browser and the CLI.

InputExpected behavior
[]One line: no readings
[30.0] with limit 30.0Total 30 and count 0
[28.0, 31.5, 30.0, 34.0] with limit 30.0Total 123.5 and count 2

Take it further

Change the function to count readings inside an inclusive range. Accept low and high parameters and combine two comparisons with &&. Test both endpoints, a value outside each end, and an empty vector. Decide how a caller should handle low greater than high.

Try the program

Output Ready
Expected output
total: 123.5
above limit: 2
no readings
Open in the full playground →

Practice

Replace only the empty batch in main with a single reading equal to the limit. Predict the complete report.

Hint

This batch takes the nonempty branch but adds nothing to the above-limit count.

Worked solution

The first batch is unchanged. For the second, sum returns 30 and the strict comparison leaves hits at zero.

Find

report([], 30.0);

Replace with

report([30.0], 30.0);

Output after the change

total: 123.5
above limit: 2
total: 30
above limit: 0