Anonymous user
Averages/Arithmetic mean: Difference between revisions
changed requirement regarding empty input, worded so it doesn't invalidate existing solutions; changed C example to *not* return 0 on such input.
(→{{header|ACL2}}: Returns 0 for the empty list even with guard checking enabled.) |
(changed requirement regarding empty input, worded so it doesn't invalidate existing solutions; changed C example to *not* return 0 on such input.) |
||
Line 1:
{{task|Probability and statistics}}Write a program to find the [[wp:arithmetic mean|mean]] (arithmetic average) of a numeric vector.
See also: [[Median]], [[Mode]]
Line 296:
=={{header|C}}==
Compute mean of a <code>double</code> array of given length. If length is zero, does whatever <code>0/0</code> does (usually means returning <code>NaN</code>).
<lang c>#include <stdio.h>▼
▲#include <stdio.h>
▲double mean(double *p, unsigned qty)
{
double
int i;
return 0;▼
▲ for (i = 0; i < qty; ++i)
▲ total += p[i];
▲ return total / qty;
}
int main(void)
{
int i, len;
for (len = 5; len >= 0; len--) {
printf("mean[");
for (i = 0; i < len; i++)
printf(i ? ", %g" : "%g", v[i]);
}▼
▲ printf("%lg\n", mean(test, 6));
}</lang>{{out}}<pre>
mean[1, 2, 2.718, 3, 3.142] = 2.372
▲}
mean[1, 2, 2.718, 3] = 2.1795
</lang>▼
mean[1, 2, 2.718] = 1.906
mean[1, 2] = 1.5
mean[1] = 1
mean[] = -nan
=={{header|C sharp}}==
|