home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / f / funnel-x.zip / examples / ex16.fw < prev    next >
Text File  |  1992-05-27  |  2KB  |  67 lines

  1. @!---------------------------------------!
  2. @!  Start of FunnelWeb Example .fw File  !
  3. @!---------------------------------------!
  4.  
  5. @t vskip 40 mm
  6. @t title titlefont centre "Powers:"
  7. @t title titlefont centre "An Example of"
  8. @t title titlefont centre "A Short"
  9. @t title titlefont centre "FunnelWeb .fw File"
  10. @t vskip 10 mm
  11. @t title smalltitlefont centre "by Ross Williams"
  12. @t title smalltitlefont centre "26 January 1992"
  13. @t vskip 20 mm
  14. @t table_of_contents
  15.  
  16. @A@<FunnelWeb Example Program@>
  17.  
  18. This  program writes  out each  of the  first @{p@}  powers of  the first  @{n@}
  19. integers. These  constant parameters are located  here so that they  are easy to
  20. change.
  21.  
  22. @$@<Constants@>==@{@-
  23. n : constant natural := 10;     -- How many numbers? (Ans: [1,n]).
  24. p : constant natural :=  5;     -- How many powers?  (Ans: [1,p]).@}
  25.  
  26. @B Here is  the outline of the  program. This FunnelWeb file  generates a single
  27. Ada output file  called @{Power.ada@}. The main program consists  of a loop that
  28. iterates once for each number to be written out.
  29.  
  30. @O@<ex16.out@>==@{@-
  31. @<Pull in packages@>
  32.  
  33. procedure example is
  34.    @<Constants@>
  35. begin -- example
  36.    for i in 1..n loop
  37.       @<Write out the first p powers of i on a single line@>
  38.    end loop;
  39. end example;
  40. @}
  41.  
  42. @B In this section,  we pull in the packages that this program  needs to run. In
  43. fact, all we need is the IO package so that we can write out the results. To use
  44. the IO package, we first of all need  to haul it in (@{with text_io@}) and then
  45. we need to make all its identifiers visible at the top level (@{use text_io@}).
  46.  
  47. @$@<Pull in packages@>==@{with text_io; use text_io;@}
  48.  
  49. @B Here is  the bit that writes out  the first @{p@} powers of  @{i@}. The power
  50. values  are  calculated  incrementally  in  @{ip@}  to  avoid  the  use  of  the
  51. exponentiation operator.
  52.  
  53. @$@<Write out the first p powers of i on a single line@>==@{@-
  54. declare
  55.    ip : natural := 1;
  56. begin
  57.    for power in 1..p loop
  58.       ip:=ip*i;
  59.       put(natural'image(ip) & " ");
  60.    end loop;
  61.    new_line;
  62. end;@}
  63.  
  64. @!---------------------------------------!
  65. @!   End of FunnelWeb Example .fw File   !
  66. @!---------------------------------------!
  67.