# ICE: adding consecutive elements in an array

I was hoping to do something more complicated, but I ran out of time…

Here’s the exercise: Take an array and build a new array by adding consecutive elements in the original array.
For example, given this array:

`1 3 2 4 7 5 6 9 11 1 3 9`

construct this array:

`1+3, 3+2, 2+4, 4+7, ..., 3+9`

Summing up three consecutive elements will hopefully be more interesting…

## 4 thoughts on “ICE: adding consecutive elements in an array”

1. is this the same exercise outlined a few times on your blog? i am confused cause it’s title suggests that but the result looks like its taking two arrays and summing the corresponding indices

• I don’t think it is the same, but it is possible I’ve just forgotten.

This time I wanted to do this in ICE:

```a = [1, 3, 2, 4, 7, 5, 6, 9, 11, 1, 3, 9]
b =[]

for i in range( len(a)-1 ):
b.append( a[i] + a[i+1] )
```
2. What do you do with the last element of the array. 9 will add to what? Do you go back to the first element of the array, add zero or skip the last element?

Will the end result be an array of the same size or one that is shorter?

• Hi Daniel

The new array is one element shorter.
I drop the last element, because I’m doing a[i] + a[i+1] for all elements in the original array.

I suppose I could just add nothing to the 9 and leave it in.