home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / basic / basictut.ark / TUTR.08 < prev    next >
Encoding:
Text File  |  1985-02-10  |  7.0 KB  |  136 lines

  1. 3000    ' tutor 8        Ver 2.0        01AUG83
  2. 3010    GOSUB 37100
  3. 3020    T5$ = SPACE$(5) : T10$ = SPACE$(10) : T15$ = SPACE$(15) :VT$ = CHR$(11)
  4. 3030    GOSUB 37100
  5. 3040    PRINT T10$;"   OK. We'll try a problem: Required: compute successive values"
  6. 3050    PRINT T10$;"of U*(U+1)^2 for uniformly increasing 'U' values, but limiting"
  7. 3060    PRINT T10$;"U*(U+1)^2 to 10 or less. The computer will decide how many 'U'"
  8. 3070    PRINT T10$;"values it will use. It will keep increasing 'U' by 'delta U'"
  9. 3080    PRINT T10$;"until U*(U+1)^2 reaches 10, then stop. The program shown "
  10. 3090    PRINT T10$;"below will accomplish this.";CRLF$
  11. 3100    PRINT "100 LET U = 0" 
  12. 3110    PRINT "110 PRINT U, U * (U + 1)^2"
  13. 3120    PRINT "120 LET U = U + 1"
  14. 3130    PRINT "130 IF U * (U + 1)^2< = 10 THEN 110"
  15. 3150    PRINT "140 END"
  16. 3160    PRINT
  17. 3170    PRINT T10$;"   Study the program and see if you can tell me what line"
  18. 3180    PRINT T10$;"number is the beginning of the loop. Which would you say? ";
  19. 3190    GOSUB 40900 : L$ = CVTSTR$
  20. 3200    PRINT
  21. 3210    IF L$ = "110" THEN 3240
  22. 3220    PRINT T10$;"Try another number."; VT$;VT$;CR$;
  23. 3230    GOTO 3180
  24. 3240    PRINT VT$;VT$;VT$;CR$;EES$;
  25. 3245    PRINT T10$;"Fine. And which is the last line in the loop? ";
  26. 3250    GOSUB 40900 : T$ = CVTSTR$ : PRINT 
  27. 3260    IF T$ = "130" THEN 3280
  28. 3270    PRINT T10$;"Study the program and try again!"; VT$;VT$;CR$;
  29. 3275    GOTO 3245
  30. 3280    PRINT T10$;"  That's the one. Notice we used a 'delta U' of unity (line 120);"
  31. 3290    PRINT T10$;"We could have used other incremental values, which would have"
  32. 3300    PRINT T10$;"changed the number of times through the loop. Here are three "
  33. 3310    PRINT T10$;"outputs based on different values of 'delta U' shown in line 120"
  34. 3320    GOSUB 37000 : GOSUB 37100
  35. 3330    PRINT T10$;"For '120 LET U = U + 1' we get:   For '120 LET U = U + .2' we get:"
  36. 3340    PRINT
  37. 3350    PRINT T10$;"     U     U * (U + 1)^2                 U     U * (U + 1)^2"
  38. 3360    PRINT T10$;"     0              0                    0              0"
  39. 3365    PRINT T10$;"     1              4                   .2             .288"
  40. 3370    PRINT T10$;"                                        .4             .784"
  41. 3380    PRINT T10$;"For '120 LET U = U + .5' we get:        .6             1.536"
  42. 3390    PRINT T10$;"                                        .8             2.592"
  43. 3400    PRINT T10$;"     0              0                   1.             4."
  44. 3410    PRINT T10$;"     .5             1.125               1.2            5.808"
  45. 3420    PRINT T10$;"     1              4                   1.4            8.064"
  46. 3430    PRINT T10$;"     1.5            9.375";CRLF$
  47. 3440    PRINT T10$;"   In each case, 'U' is the left-hand column and 'U*(U+1)^2' is the"
  48. 3450    PRINT T10$;"right-hand column. Notice - the largest number in the right-hand"
  49. 3460    PRINT T10$;"column is never over 10 (130 IF U * (U + 1)^2< = 10 THEN 110.)"
  50. 3465    PRINT T10$;"The computer has seen that one more 'U' would be too much,"
  51. 3470    PRINT T10$;"and has taken action to halt the loop."
  52. 3480    GOSUB 37000 : GOSUB 37100
  53. 3490    PRINT T10$;"   Another way to 'loop' uses an instruction requiring a predeter-"
  54. 3500    PRINT T10$;"mined number of loops - i.e., the computer is instructed exactly how"
  55. 3510    PRINT T10$;"many iterations it is to make. Here is an example:";CRLF$
  56. 3520    PRINT "160 FOR J = 2 TO 2.5 STEP .1"
  57. 3530    PRINT
  58. 3540    PRINT T10$;"   How would you answer this: True or False: 'J' values control"
  59. 3550    PRINT T10$;"the number of iterations. -- Your answer? ";
  60. 3560    GOSUB 40900 : J$ = LEFT$(CVTSTR$,1)
  61. 3570    PRINT
  62. 3580    IF J$ = "1" OR J$ = "T" THEN 3610
  63. 3590    PRINT VT$;VT$;VT$;CR$;EES$;T10$;"   You should have said 'True'. You see, 'J' is the counter, and"
  64. 3600    GOTO 3620
  65. 3610    PRINT VT$;VT$;VT$;CR$;EES$;T10$;"   You're right. The 'J' value will be used to count the loop."
  66. 3620    PRINT T10$;"its starting value will be '2'. Its maximum value will be? ";
  67. 3630    GOSUB 40900 : J1$ = CVTSTR$ 
  68. 3640    PRINT
  69. 3650    IF J1$ = "2.5" THEN 3680
  70. 3660    PRINT T10$;"   No - but its maximum value is shown in the statement.";VT$;VT$;CR$;ELR$;
  71. 3670    GOTO 3620
  72. 3680    PRINT VT$;VT$;VT$;CR$;EES$;T10$;"   Good. But it may end up somewhat less, because of the size of"
  73. 3690    PRINT T10$;"the increment (change in 'J'). What increment is shown? ";
  74. 3700    GOSUB 40900 : I$ = CVTSTR$
  75. 3710    PRINT
  76. 3720    IF I$ = ".1" THEN 3750
  77. 3730    PRINT T10$;"   That's not it, but the statement tells.";VT$;VT$;CR$;
  78. 3740    GOTO 3690
  79. 3750    PRINT VT$;VT$;VT$;CR$;EES$;T10$;"   You obviously understand the statement, and probably realize"
  80. 3760    PRINT T10$;"that '160' is the statement (or line) number, 'FOR' means"
  81. 3770    PRINT T10$;"'starting value' to the machine, 'TO' says 'maximum value',"
  82. 3780    PRINT T10$"and 'STEP' means 'increment by'. "
  83. 3790    GOSUB 37000 : PRINT VT$;VT$;VT$;VT$;VT$;CR$;EES$;T10$;
  84. 3800    PRINT "   Now we have one more obstacle. The computer knows where the"
  85. 3810    PRINT T10$;"loop starts (at line 160) and everything else about it except"
  86. 3820    PRINT T10$;"where it stops - i.e., what the last instruction in the loop "
  87. 3830    PRINT T10$;"is. Do you think (1) 'LAST LINE' or (2) 'NEXT J' or (3) 'NEW J'"
  88. 3840    PRINT T10$;"is the correct last instruction? ";
  89. 3850    GOSUB 40900 : N$ = CVTSTR$
  90. 3860    PRINT  VT$;VT$;VT$;VT$;VT$;CR$;EES$;
  91. 3870    IF N$ = "2" OR (LENSTR% > 2 AND LEFT$(N$,3) = "NEX") THEN 3890
  92. 3875    PRINT
  93. 3880    PRINT T10$;EES$;"   Well, maybe that was too tricky. It should be 'NEXT J', and we"
  94. 3885    GOTO 3900
  95. 3890    PRINT T10$;EES$;"   That's the one, all right. The word 'NEXT' is the clue. so we"
  96. 3900    PRINT T10$;"put a line number on it which sequences it as the last line of"
  97. 3910    PRINT T10$;"the loop. The computer then 'backtracks' to the beginning of"
  98. 3920    PRINT T10$;"the loop (line 160 here) and 'loops' until 'J' says to stop."
  99. 3930    GOSUB 37000 : GOSUB 37100
  100. 3940    PRINT T10$;"   For the previous example, suppose we wanted 4 values, regardless"
  101. 3950    PRINT T10$;"of the function's size. Let's count with 'C'. We'll write:"
  102. 3960    PRINT
  103. 3970    PRINT "100 LET U = 0"
  104. 3980    PRINT "110 FOR C = 1 TO 4"
  105. 3985    PRINT "120 PRINT U, U * (U + 1)^2"
  106. 3990    PRINT "130 LET U = U + .2"
  107. 4000    PRINT "140 NEXT C"
  108. 4010    PRINT "150 END"
  109. 4020    PRINT
  110. 4030    PRINT T10$;"   When 'step' is omitted (line 110) the increase (in 'C') is 1."
  111. 4040    PRINT T10$;"So now we have looked at two ways to iterate - the 'IF' loop"
  112. 4050    PRINT T10$;"and the 'FOR' loop. We'll be seeing more of them in the future."
  113. 4060    PRINT CRLF$;T10$;"Your next lesson is 'TUTR.09'"
  114. 4070    PRINT CRLF$;FNCENTER$("press any key for menu");
  115. 4080    FOR CT = 1 TO 22 : PRINT CHR$(8); : NEXT CT : GOSUB 40800
  116. 4090    GOTO 39999
  117.  
  118. 37000    'press any key pause
  119. 37010    PRINT T10$;CRLF$;CRLF$;FNCENTER$("press any key to continue");
  120. 37020    FOR CT= 1 TO 25 : PRINT CHR$(8); : NEXT CT
  121. 37030    GOSUB 40800
  122. 37099    RETURN
  123.  
  124. 37100    ' heading
  125. 37110    PRINT CLS$;FNPOSCUR$(4,1);FNCENTER$("Tutor -- Lesson 8");CRLF$
  126. 37199    RETURN
  127.  
  128. 38000    ' converted for Non-linear Systems, Kaypro Educational Division
  129. 38010    ' by t.crew , Simple Software, San Diego CA
  130. 38020    ' has to be run with mentutr to get all variables
  131. 38030    ' tutor lesson 8 version 2.0  released to nls 1 aug 83
  132.  
  133. 39999    RUN "MENU" 'end of overlay
  134.  
  135. tutr to get all variables
  136. 38