tags:

views:

111

answers:

4

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 their find the mean value of the 5 succeeding 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[] = {1,2,3,4,...,30};
    char *store;
    float sum;
    
    
    for(int j=0;j<tabdata[30-1];j++)
       sum += tabdata[j];
    if (j=5)
    {
       float mean= sum/5;
       store[j]=mean;
       sum=0;
       for(i=j;i>tabdata[30-1];i++)
           sum +=tabdata[i];
    

if (j=5) --- ---- ....need help to complete this loop please.

+1  A: 

Just add 1/5 of the next element and subtract 1/5 of the first element in the current window at every step. The only thing you need to worry about is floating point precision.

R..
A: 

I think that what you might want to use is the modulus operator (%) which will give you the rest of the division. You will get something like this:

if ((j+1) % 5 == 0)
{
   float mean= sum/5;
   store[j/5]=mean;
   sum=0;
}

This way, at every 5 iterations, the code will be executed.

Alexandre Deschamps
A: 

Haven't tested this but should work:

float tabdata[] = {1, 2, 3, ..., 30};
float *result;
float sum;
int count = sizeof(tabdata)/sizeof(tabdata[0]);

result = (float *)malloc((count - 4) * sizeof(float));
for (int j = 0; j < count - 5; j++)  {
   sum = 0;
   for (int k = j; k < j + 5; k++)  {
     sum += tabdata[k];
   }
   result[j] = sum / 5;
}

// ...
free(result);
dark_charlie
in (int k = j; k < 5; k++) loop condition should be k < j+5
Vladimir
Also in-efficient - it calculated the sum for each point, when it could just keep an updated sum as it goes.
Douglas Leeder
While it's inefficient, it does avoid problems with loss of precision where the effects of adding 1/5 of a value and later subtracting 1/5 of that same value might not cancel out.
R..
Thank you all, you guys have being wonderful.You saved me a lot of useful time.
chriscol
+1  A: 

You can fix up the sum as you go:

#include <stdlib.h>
#include <stdio.h>

int main()
{
    float tabdata[] = {1,1.5,1.8, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,161,7,18,19,20,21,30};
    float* result;
    float sum;
    int count = sizeof(tabdata)/sizeof(tabdata[0]);
    int i;

    result = (float *)malloc((count - 4) * sizeof(float));
    /* Initialise with the first five elements */
    for (i=0;i<5;i++)
    {
        sum += tabdata[i];
    }
    result[0] = sum / 5.0;

    for (i=5;i<count;i++)
    {
        sum -= tabdata[i-5];
        sum += tabdata[i];
        result[i-4] = sum / 5.0;
    }

    for (i=0;i<count-4;i++)
    {
        printf("%f\t%f\n",tabdata[i],result[i]);
    }
    for (;i<count;i++)
    {
        printf("%f\n",tabdata[i]);
    }

    free(result);
}
Douglas Leeder