- Joined
- Jun 8, 2022
- Messages
- 4
- Reaction score
- 0
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.
// 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 muchI 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 }
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.