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.

Recommended Answers

All 6 Replies

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;
    }

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

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
}

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

Member Avatar for engg_fahd

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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.