# Update an array

posted 5 years ago

Something like a rolling buffer? You need to keep a count of the calculations you do, and when it gets to 10, reset to 0. Then store the result at the index shown by the calculation count.

Another thing you can do is never reset the calculation count, and compute the index as count % 10.

Another thing you can do is never reset the calculation count, and compute the index as count % 10.

posted 5 years ago

If each time reset the counter, that's mean the next time i will add something, will go to array[0] right? I looking something like if array is full and want to add one more, very first element deleted, move rest of elements one place up and and the new element at the last place.

Also, i didnt get your point with remainder.

Also, i didnt get your point with remainder.

posted 5 years ago

You don't reset the counter to 0 each time ... just every 10th time.

The remainder operator lets you cycle through the array without checking the value of the counter and setting it back to 0 when it gets too big. That's because 10 % 10 is 0, 11 % 10 is 1 and so on. Any non-negative integer % 10 will be a an integer from 0 to 9, just like the indexes of your array.

If you want to shift the results every time, then loop thru the indexes from 1 to 9 and copy the array values to indexes 0 to 8, then put the new result in the array at index 9.

The remainder operator lets you cycle through the array without checking the value of the counter and setting it back to 0 when it gets too big. That's because 10 % 10 is 0, 11 % 10 is 1 and so on. Any non-negative integer % 10 will be a an integer from 0 to 9, just like the indexes of your array.

If you want to shift the results every time, then loop thru the indexes from 1 to 9 and copy the array values to indexes 0 to 8, then put the new result in the array at index 9.