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
2 replies to this topic
#1
Posted 31 May 2011 - 03:12 PM
"Programming is like sex. One mistake and you have to support it for the rest of your life."
-Michael Sinz|
|
|
#2
Posted 01 June 2011 - 06:00 AM
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
Posted 01 June 2011 - 02:29 PM
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 Sinz1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









