Can some one please help me on this issue as I have spent time going around it without making any headway.
I have data in an array of size say 3O.

1. I want to take the first five elements of the array, find their mean value. Store the value in another array
2.Then move to the second element of the array,from thier find the mean value of the 5 succeding elements.store the value in the array as above.
3.Then wove to the 3rd element,do the same thing above till the last element which is 30 in this case.

float tabdata[30] = {1,2,3,4,...,30};
char *store;
float sum;

int count=0;
for(int j=0;j<tabdata[30-1];j++)how to
   sum += tabdata[j];
if (count=5)
 {
float mean= sum/5;
 store[j]=mean;
sum=0;
count=0;
}
count=j+1;

 
    ---
    ----
    ....need help to complete this loop please.
Re: How to take a moving average in an array 80 80

So for 30 elements size of average array will be 25 ? You can try something like this:

for(int i = 0; i < 5; i++)
            sum += arr[i];
    avg[0] = sum/5;
    
    int j = 1;   
    for(int i = 5; i < arrLen; i++){
         sum = sum+arr[i]-arr[i-5];
         avg[j++] = sum/5;
    }
Re: How to take a moving average in an array 80 80

Thank you faxo, I think I am very comfortable from where you left.I will take it up from there.

Re: How to take a moving average in an array 80 80

This is how I would do it

int calculateAverage(int* a, int startPoint)
{
     for(int i= startPoint; i<startPoint+5;i++)
         //calculate the average and store it in sum

     return sum
}

int main()
{
     int arr[30]={1,2,3...30};

     for(int i=0;i<30;i++)
       average[i]= calculateAverage(arr,i);

    // Use some error handling for the cases when i becomes > 25
}
Re: How to take a moving average in an array 80 80

yah!.thanks abhimanipal thats saves a lot of computational lines.

Re: How to take a moving average in an array 80 80

If you already have data stored on the array,how can you overwrite the sum after every 5 numbers?

Re: How to take a moving average in an array 80 80

If you already have data stored on the array,how can you overwrite the sum after every 5 numbers?

I dont think you got the logic of the program that I wrote ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.