Jump to content

Update an array

- - - - -

  • Please log in to reply
2 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I have an integer array size of ten. Im trying to create a method that each time, will store in array the ten recent calculations. For example if i do 40 calculations in array i want to each time storing the 10 most recent. How i can achieve this?

thank you

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
The way I would do this is by thinking of your array as a closed loop--that is, if you go beyond the bounds of either the beginning or end, you wrap around to the beginning again. (This wrap around would have to be manually programmed by either checking for index below zero or above size-1.) Then, create a placeholder variable that will point to the next index following the one holding the most recent calculation, and initialize it to zero. Each time you perform a calculation, store the calculation at your placeholder index, and increment the placeholder. If it wraps around, set it back to zero. Each time, you are overwriting the value that was previously there, so you will only be storing the 10 most recent calculations. When it comes time to retrieve them in order, simply use a for loop running 10 times, and in the body of the loop, decrement your placeholder (keeping in mind the wrap-around), and retrieve the calculation at that position.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
That's the way did it finally, start thinking like my master ;) Thank you

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users