# C query

#### Rahul Kumar

Suppose i have predefine array of elements. I want to calculate the sum of first five elements, then display it & then calculate the sum of next elements & so on till last.

#### AngleWyrm C:
``````// given an array and an index, return sum of next five elements
double sumNextFiveElements(double[] array, int index)``````

Last edited:

#### Rahul Kumar

Can you please provide me the code

#### AngleWyrm

I write in Java not C, so I cannot give you the code, only an algorithm.
Java:
``````// loop through segments of the array (assumes array length divisible by 5)
for(int offset = 0; offset < array.size(); offset += 5){
// gather elements of segment
for(int index = offset; index < offset + 5; index++){
total += array[index]
}
// could print total here, and then reset total if desired
}``````

Last edited:

#### Rahul Kumar

It's ok you can provide java code too

#### Rahul Kumar

I write in Java not C, so I cannot give you the code, only an algorithm.
Java:
``````// loop through segments of the array (assumes array length divisible by 5)
for(int offset = 0; offset < array.size(); offset += 5){
// gather elements of segment
for(int index = offset; index < offset + 5; index++){
total += array[index]
}
// could print total here, and then reset total if desired
}``````
Thank you so much

#### sakshijn

C++:
``````for(int i= 0; i < arr.size(); i += 5)
{
for(int j = i; j < i+ 5; j++){
sum += arr[j];
}
cout<<sum<<endl;
}``````