home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l7p140 < prev    next >
Text File  |  1990-07-15  |  3KB  |  74 lines

  1.  
  2.        ╔═════════════════════════════════════════════════════╗
  3.        ║  Lesson 7 Part 140F-PC 3.5 Tutorial by Jack Brown  ║
  4.        ╚═════════════════════════════════════════════════════╝
  5.  
  6.                   ┌────────────────────────┐
  7.                   │  The  Average  Array   │
  8.                   └────────────────────────┘
  9.  
  10. Here is an application of the compiler extension words CREATE...DOES>
  11. to form an intelligent data structure.  The idea is to have an array
  12. that automatically maintains the running average of its non zero
  13. elements in slots 1 through n  in array position 0. This means that
  14. every time a number is stored in the array the word itself must be smart
  15. enough to know that the store operation is being performed and then
  16. calculate the average of its non-zero elements and update the value of
  17. the average in position 0 of the array.
  18.  
  19. Remember that this idea of extending the compiler is new and unheard of
  20. in other languages.  How would you do this in C?  Of what use would this
  21. type of intelligent array be?  Perhaps you could use it in you stock
  22. monitoring program.  You could use one of these intelligent arrays to
  23. keep track of the performance of your favorite stocks.  As you add each
  24. daily or weekly value of the stock the average would be automatically
  25. calculated.  You could even extend this idea to have the array keep
  26. track of more information.  How about also maintaining a moving average
  27. of the last three or ???  non zero entries made into the array?  Well I
  28. will leave the extensions up to you.  Let me know if you find this type
  29. of structure useful or if you come up with any interesting variations.
  30.  
  31.  
  32. \ Finds the average of the n elements at adr+4 and stores the
  33. \ result at adr+2.  Only non zero elements are counted.
  34. \  AARRAY --> : n : avg : x1 : x2 : .....  : xn :
  35. : AAVERAGE  ( adr    -- )
  36.       DUP 4 + OVER
  37.       @ 2*
  38.       OVER + SWAP
  39.       0 0 2SWAP
  40.       ?DO I @  DUP 0<>
  41.           IF   1 D+ ELSE DROP THEN 2 +LOOP
  42.        / SWAP 2+ ! ;
  43.  
  44. \ This a flag that indicates access mode.
  45. \ false is fetch mode, the default, and true is store mode.
  46. VARIABLE (:=)   (:=) OFF
  47. : :=  (:=) ON ;
  48. : AARRAY   ( n   - )
  49.      CREATE  DUP , 1+ 0
  50.      ?DO 0 , LOOP
  51.      DOES> DUP >R (:=) @  >R (:=) OFF
  52.      SWAP 1+ 2* +   R>
  53.      IF   ! R> AAVERAGE
  54.      ELSE @ R> DROP
  55.      THEN ;
  56.  
  57. \ Examples of the AARRAY
  58. 5 AARRAY junk  <enter> ok   \ Create an average array with 5 elements.
  59. 12 := 2 junk <enter>  ok    \ Set element number 2 to the value 12
  60. 0 junk . <enter> 12  ok     \ Fetch element 0, the current average.
  61. 8 := 1 junk  <enter> ok     \ Set element number 1 to 8.
  62. 0 junk . <enter> 10  ok     \ Now element 0 is 10, the average of 8 and 12.
  63. 1 := 4 junk  <enter> ok     \ Set element number 4 to 1.
  64. 0 junk . <enter> 7  ok      \ And of course the average is now 7!
  65.  
  66. \ Now can you think of anything else you might like an intelligent
  67. \ array to do?
  68.  
  69. ┌───────────────────────────────────┐
  70. │  Please Move to Lesson 7 Part 150 │
  71. └───────────────────────────────────┘
  72.  
  73.  
  74.