我需要根据跨度的大小计算平均值,该平均值移动一个元素并输出直到最后的元素的平均值。
#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(count=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;
}
最佳答案
提高编译器的警告级别!
对 moving_average()
的调用中存在语法错误,您的编译器应该已捕获该错误。
并且 sum
未初始化,如果设置正确,编译器应警告您。
关于c - 需要帮助制作计算移动平均线的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5735298/