开发者

Help needed with making a program that calculates moving average

开发者 https://www.devze.com 2023-02-27 19:58 出处:网络
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.

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 to moving_average
  • Set sum = 0 before for(k=count; k<(count+span); k++)
  • add a \n in your printf
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号