审查这个用于计算传感器读数平均值的生成函数。
最多接收 16 个读数,忽略负数,通过 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;
}
生成代码仅作示例,不代表任何特定模型