I need to calculate average with the size of span and which is moving by one element and outputting the average of elements till the end.
#include <stdio.h>
#include <stdlib.h>
void moving_average (int size, double a[], int span)
{
int k;
double sum;
int n;
int count;
double output;
n=size-span;
for(co开发者_运维百科unt=0;count <= n;count++)
{
for(k=count; k<(count+span); k++)
sum+=a[k];
output=sum/span;
printf("%lf", output);
}
}
int main(void)
{
double array[]={10,9,15,6,7};
moving_average(5,array[], 2);
return 0;
}
Increase the warning level of your compiler!
There's a syntax error in the call to moving_average()
which your compiler should have caught.
And sum
is not initialized which your compiler should warn you about if it is properly setup.
- Remove the brackets after
array
in the call tomoving_average
- Set
sum = 0
beforefor(k=count; k<(count+span); k++)
- add a
\n
in your printf
精彩评论