bhylu.com

Advertisements

C Survey Report Project

Build a complete batch report that validates each record and reports count, minimum, maximum and mean.

Your brief

Build a report from one day of survey depths. The input contains an invalid reading and a valid zero, so a correct result cannot come from blindly summing the whole array or treating zero as absent. Keep the accepted count separate from the source length. Initialize minimum and maximum from the first accepted reading instead of arbitrary guesses. State an empty-batch policy before calculating a mean. This fixed-size project stays within a small bounded total; a larger ingestion tool needs checked accumulation and a record limit.

Run it locally

Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.

cc -std=c17 -Wall -Wextra -Wpedantic -Werror survey-project.c -o lesson

On Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.

Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.

Worked solution

Show the worked solution
C17 · survey-project.c

#include <stdio.h>

int main(void) {
    const int input[] = {18, -4, 0, 32, 26};
    size_t accepted = 0;
    long total = 0;
    int minimum = 0, maximum = 0;
    for (size_t i = 0; i < sizeof input / sizeof input[0]; ++i) {
        int depth = input[i];
        if (depth < 0 || depth > 500) { continue; }
        if (accepted == 0 || depth < minimum) { minimum = depth; }
        if (accepted == 0 || depth > maximum) { maximum = depth; }
        total += depth; ++accepted;
    }
    if (accepted == 0) { puts("No valid readings"); return 0; }
    printf("Accepted: %zu\nMin: %d; max: %d\nMean: %.2f mm\n", accepted, minimum, maximum, (double)total / (double)accepted);
    return 0;
}

Expected result

Accepted: 4
Min: 0; max: 32
Mean: 19.00 mm

The four accepted values sum to seventy-six. The first accepted reading establishes both extrema; later values update them. The empty check happens before division. This project deliberately separates domain validation from reporting. To extend it, move accumulation into a stateful helper, add table-driven tests and parse a bounded input format before feeding values to that helper.

Extend and test

Extract a Report structure and add an accept function. Test one reading, all invalid readings, duplicate extrema and the maximum allowed batch size.

For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.

Advertisements