Skip to content

Instantly share code, notes, and snippets.

@rbranson
Created August 31, 2009 18:28
Show Gist options
  • Select an option

  • Save rbranson/178630 to your computer and use it in GitHub Desktop.

Select an option

Save rbranson/178630 to your computer and use it in GitHub Desktop.
/* omg -- a C program to average things -- by Rick Branson -- public domain 'n shit */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUFSZ 1024 * 32
#define ARRAY_ALLOC_SZ 1024 * 32
int main(int argc, char **argv)
{
int alloced = 0;
int i = 0;
int x;
float *nums = NULL;
float tmp = 0;
char s[BUFSZ];
char *s2;
double sum = 0;
while (fgets(s, BUFSZ, stdin))
{
if (i == alloced)
{
alloced += ARRAY_ALLOC_SZ;
nums = realloc(nums, alloced * sizeof(float));
}
s2 = s;
/* handle leading spaces */
if (s[0] == ' ')
{
while (*s2++)
{
if (*s2 != ' ')
break;
}
}
sscanf(s2, "%f", &tmp);
if (tmp)
{
nums[i++] = tmp;
tmp = 0;
}
}
for (x = 0; x < i; x++)
{
sum += nums[x];
}
printf("%.10f\n", sum / i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment