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.
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
}
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
}
for(int i= 0; i < arr.size(); i += 5)
{
for(int j = i; j < i+ 5; j++){
sum += arr[j];
}
cout<<sum<<endl;
}
Ask a Question
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.