Review generated reading average

from C fundamentals
C23 (GCC 13.3.0) advanced 10 min 4 issues to find

Review this generated function for averaging sensor readings.

Accept at most 16 readings, ignore negative readings, write the average through result, and report invalid pointers, oversized input, or an empty filtered result.

C
#include <stddef.h>

int average_nonnegative(const int *readings, size_t count, double *result) {
    int total;
    size_t accepted = 0;

    for (size_t index = 0; index <= count; ++index) {
        if (readings[index] >= 0) {
            total += readings[index];
            ++accepted;
        }
    }

    if (accepted == 0) {
        return 0;
    }

    *result = total / accepted;
    return 1;
}

generated code is illustrative, not from any one model

Open in playground
Report an error