home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol022 / lesson4 < prev    next >
Encoding:
Text File  |  1984-04-29  |  4.1 KB  |  138 lines

  1. ^I ^I Input and Output; the complete program
  2. ^P
  3. 4.1 INPUT AND OUTPUT OF DATA
  4. ^p
  5. So far we have seen how to write constant definitions, variable
  6. declarations and assignment statemments. The latter can be used
  7. to compute new values from values which have been computed
  8. previously and from constants. These program elements allow us
  9. to specify computations of a restricted kind but they provide
  10. no means to vary the data on which the commputations are
  11. performed.
  12. ^p ^N
  13. Example 4.1
  14. ^N ^N
  15. ^I Assuming the variable declaration
  16. ^p
  17. A, D, N, SUM : INTEGER
  18. ^P
  19. the following statements, obeyed in sequence, compute the sum of
  20. the first N terms of the arithmetic series A, A+D, A+2*D, ....,
  21. given the values 1 for A, 2 for D and 10 for N:
  22. ^p
  23.    A := 1 ^N
  24. ^I D := 2 ; ^N
  25. ^I N := 10 ; ^N
  26. ^I SUM := N * (2*A + (N-1)*D DIV 2
  27. ^p
  28. If we wish to compute the series sum for different values of A,
  29. D and N, we have no alternative but to modify the "program"
  30. itself, specifically its first three statements.
  31. ^N ^N
  32. 4.2 BASIC INPUT
  33. ^P
  34. The role of an input instruction is to read an item of data and
  35. store it in a variable, so that subsequently it can be used in
  36. some computation.
  37. In PASCAL this role is performed by the READ statement.
  38. ^N ^N
  39. Example 4.1 (continued)
  40. ^P
  41. We can vary the data on which our "program" works by replacing
  42. the first three assignment statements by READ statements:
  43. ^P
  44.    READ (A) ^N
  45. ^I READ (D) ^N
  46. ^I READ (N) ^N
  47. ^I SUM := N * (2*A + (N-1)*D) DIV 2
  48. ^P
  49. Now if we supply the following input data, for example:
  50. ^P
  51.  -3 4 100<cr> ^N
  52. ^P
  53. The data item -3 will be read and assigned to A, then the data
  54. 4 will be read and assigned to D, then the data item 100 will
  55. be read and assigned to N. We can make the "program" perform
  56. a different calculation simply by running it again with different
  57. input.
  58. ^P
  59. In general, the effect of the READ statement READ(V),
  60. where V is an INTEGER variable, is to scan forwards through
  61. the input data (skipping blank characters) until a data
  62. item (which must be an integer number, possible signed)
  63. is then assigned to V.
  64. ^P
  65. The three consecutive READ statements in Example 4.1
  66. could be combined into a single READ statement with the
  67. same effect:
  68. ^P
  69. READ (A, D, N)
  70. ^P
  71. A, D and N are called parameters of the READ statement. A
  72. READ statement may have any number of parameters, all of
  73. which must be variables, and for each of these variables
  74. one data item is read.
  75. ^N ^N
  76. 4.3 BASIC OUTPUT
  77. ^P
  78. The role of an output instruction is to get results out of
  79. the computer in some suitable form, eg. printed on paper or
  80. displayed on a screen.
  81. In PASCAL this role is performed by the WRITE statement.
  82. ^N ^N
  83. Example 4.2
  84. ^P
  85. Continuing from Example 4.1, we could write out the value
  86. of the series sum by the following WRITE statement:
  87. ^P
  88.     WRITE (SUM)
  89. ^P
  90. The following statements, executed in sequence, will
  91. read values for A, D and N, compute the series sum, and
  92. write out the values of A, D, N and the series sum:
  93. ^P
  94.    READ (A, D, N) ^N
  95. ^I SUM := N * (2*A + (N-1)*D) DIV 2 ^N
  96. ^I WRITE (A, D, N, SUM)
  97. ^P
  98. For example, if the input data is:
  99. ^P
  100. 10 -2 8<cr>
  101. ^P
  102. then the output would look like this:
  103. ^P
  104. ........10........-2.........8........24
  105. ^P
  106. Observe that a WRITE statement, like a READ statement,
  107. may have any number of parameters. However, each parameter
  108. of WRITE may be an expression, not necessarily a simple
  109. variable and it is the value of this expression which is
  110. written.
  111. ^N ^N
  112. ^P
  113. Assuming that M and N are INTEGER variables and that values have
  114. been assigned to them, the following statement writes the values
  115. of M and N followed by their sum, difference and product:
  116. ^P
  117. WRITE (M, N, M+N, M-N, M*N) ^N
  118. ^N ^N
  119. 4.4 THE COMPLETE PROGRAM
  120. ^p
  121. To buld a complete Pascal program, we must collect together all the
  122. necessary definitions, declarations and statements.
  123. ^P ^N
  124. Example 4.4
  125. ^P
  126. Here is an example of a complete Pascal program which performs a
  127. simple tax calculaton:
  128. ^p ^n ^n
  129. ^I PROGRAM COMPUETAX ;
  130. ^p ^n ^n
  131. Now let us try Exercise 4.2 with a short program.
  132. ^P
  133. ^P
  134. You may terminate this exercise by entering
  135. 9 9 9<cr>
  136. ^N
  137. ^N
  138. ^$