projects:test1-2.blk
MID TERM EXAM 2
Screen 0 not modified 0 \ 13:36JWB02/16/86 1 \ Last change: Screen 011 20:25JWB02/17/86 2 3 MATHEMATICS 495 4 5 6 INTRODUCTION TO THE FORTH PROGRAMMING LANGUAGE 7 8 9 MID TERM EXAM 10 11 FEBRUARY 18, 1986 12 13 TOTAL MARKS = 25 14 15 Screen 1 not modified 0 \ QUESTION 1 ( 5 MARKS ) 13:45JWB02/16/86 1 Show the resulting stack after return is pressed. 2 3 Example: 54 21 13 SWAP DROP 4 Answer: 54 13 <top of stack. 5 6 a) 13 17 23 31 ROT ROT 7 8 b) 13 31 17 23 OVER OVER 9 10 c) 23 17 13 2 PICK 2 PICK 2 PICK 11 12 d) 11 5 -14 5 /MOD 13 14 e) 25 50 10 */ 5 + 10 / 15 Screen 2 not modified 0 \ QUESTION 2 ( 5 MARKS ) 16:13JWB02/16/86 1 Design the FORTH word EIGHT that will draw the outline of the 2 number eight on the display. The outline must use the 3 word WHITE ( or WW ) from the CHECKER-BOARD program. 4 5 : WHITE 177 EMIT ; XXXXXX 6 X X 7 The word EIGHT takes no stack parameters. X X 8 The output should look exactly like the X X 9 figure at right except that it will not XXXXXX 10 be made from X's. To obtain full marks X X 11 you should demonstrate your knowledge of X X 12 factoring (ie your definition of EIGHT X X 13 should not be one great big word!). XXXXXX 14 15 Screen 3 not modified 0 \ QUESTION 3 ( 5 MARKS ) 16:15JWB02/16/86 1 A closed rectangular tank has a square base, b cm by b cm, 2 and is h cm high. 3 a) Write the word AREA that takes two stack inputs base, and 4 height and leaves the surface area, a= 2bb +4bh, on the stack 5 AREA ( b h a ) 6 b) Write the word VOLUME that takes two stack inputs base, and 7 height and leaves the volume, v=bbh, on the stack. 8 VOLUME ( b h v ) 9 c) Write the word TANK that takes two stack inputs base, and 10 height and uses the words AREA and VOLUME above to produce 11 the following report when executed as shown below: 12 4 5 TANK 13 The surface area is 112 square centimeters. 14 THe volume is 80 cubic centimeters. 15 Screen 4 not modified 0 \ QUESTION 4 ( 5 MARKS ) 16:49JWB10/27/85 1 a) Show how you would create a FORTH variable called VALVE . 2 The variable VALVE will be used to contain the percentage 3 that a valve is open ( 0 is closed, 100 full open). 4 5 b) Design the word STATUS? to display the current valve state. 6 ie STATUS? displays: Valve is now XX percent open. 7 8 c) Write INITIAL-SETTING that sets variable VALVE 9 to the top stack number. 10 11 d) Write the word OPEN which adds the top stack number to VALVE 12 and word CLOSE which subtracts top stack number from VALVE . 13 e) Can the value of VALVE ever become less than 0 using CLOSE ? 14 Can the value of VALVE ever exceed 100 using OPEN ? 15 Fix OPEN and CLOSE so that VALVE is always between 0 and 100. Screen 5 not modified 0 \ QUESTION 5 ( 5 MARKS ) 16:19JWB02/16/86 1 a) Create an array called DATA that will hold 50 16bit numbers. 2 3 b) Write a word called CLEAR-DATA which initializes 4 each cell of the array DATA to zero. 5 6 c) Write a word called COUNT-DATA that counts the number of 7 non zero entries in the array and saves it in the variable N. 8 9 d) Write a word called SUM-DATA that leaves the sum of all non 10 zero entries in the array DATA in the variable SSUM . 11 12 e) Write a word called SUM-SQR that leaves the sum of squares 13 of all non zero entries in the array DATA in the variable 14 SSQR . 15 Screen 8 not modified 0 \ 32 bit square root KS 4TH DIM V4N1P9 1 2 : EASY-BITS ( drem1 partial.root1 count drem2 partial.root2 ) 3 0 DO >R D2* D2* 4 R@ - DUP 0< 5 IF R@ + R> 2* 1- 6 ELSE R> 2* 3 + 7 THEN LOOP ; 8 9 : 2'S-BIT ( drem2 proot2 drem3 proot3 ) \ get penultimate bit 10 >R D2* DUP 0< 11 IF D2* R@ - R> 1+ 12 ELSE D2* R@ 2DUP U< 13 IF DROP R> 1- 14 ELSE - R> 1+ 15 THEN THEN ; Screen 9 not modified 0 \ 32 bit square root KS 4TH DIM V4N1P9 1 : 1'S-BIT ( drem3 proot3 fullroot ) \ remainder lost 2 >R DUP 0< 3 IF 2DROP R> 1+ 4 ELSE D2* 32768 R@ DU< 0= R> THEN ; 5 \ 32-bit unsigned radicand to 16-bit unsigned square root 6 : SQRT ( ud u ) 7 0 1 8 EASY-BITS ROT DROP 6 EASY-BITS 8 2'S-BIT 1'S-BIT ; 9 \ Display square root of 16-bit number with 3 decimal places. 10 : .SQRT ( n -- ) \ n must be < 4096 11 16 * 62500 UM* 12 SQRT 0 <# # # # ASCII . HOLD #S #> 13 TYPE SPACE ; 14 : TEST 100 0 DO CR I 5 .R SPACE I .SQRT LOOP ; 15 Screen 11 not modified 0 \ ANSWER TO QUESTION 5 ( 5 MARKS ) 20:25JWB02/17/86 1 Show the resulting stack after return is pressed. 2 3 Example: 54 21 13 SWAP DROP 4 Answer: 54 13 <top of stack. 5 6 a) 13 17 23 31 ROT ROT 13 31 17 23 7 8 b) 13 31 17 23 OVER OVER 13 31 17 23 17 23 9 10 c) 23 17 13 2 PICK 2 PICK 2 PICK 23 17 13 23 17 13 11 12 d) 11 5 -14 5 /MOD 11 5 1 -3 13 14 e) 25 50 10 */ 5 + 10 / 13 15 Screen 12 not modified 0 \ ANSWER TO QUESTION NUMBER 2 ( 5 MARKS ) 20:25JWB02/17/86 1 2 : WHITE 177 EMIT ; 3 : .TOP ( -- -- ) 4 SPACE 6 0 ?DO WHITE LOOP ; 5 : .ROW ( -- -- ) 6 WHITE 6 0 ?DO SPACE LOOP WHITE ; 7 8 : EIGHT ( -- -- ) 9 CR .TOP 10 3 0 DO CR .ROW LOOP 11 CR .TOP 12 3 0 DO CR .ROW LOOP 13 CR .TOP 14 CR ; 15 Screen 13 not modified 0 \ ANSWER TO QUESTION 3 ( 5 MARKS ) 18:04JWB02/16/86 1 : AREA ( b h a ) 2 OVER DUP * 2 * ROT ROT * 4 * + ; 3 4 : VOLUME ( b h v ) 5 OVER * * ; 6 7 : TANK ( b h -- ) 8 CR OVER OVER 9 CR ." The surface area is " AREA . 10 ." square centimeters." 11 CR ." The volume is " VOLUME . 12 ." cubic centimeters." 13 CR ; 14 4 5 TANK 15 Screen 14 not modified 0 \ ANSWER TO QUESTION 4 ( 5 MARKS) 20:22JWB02/17/86 1 ( a) VARIABALE VALVE 2 3 ( b) : STATUS? ( -- -- ) 4 CR ." Valve is now " VALVE @ . ." percent open." ; 5 6 ( c) : INITIAL-SETTING ( n -- ) 7 0 MAX 100 MIN VALVE ! ; 8 9 ( d) : OPEN ( n -- ) VALVE +! ; 10 : CLOSE ( n -- ) NEGATE VALVE +! ; 11 12 ( e) : OPEN ( n -- ) VALVE @ + 100 MIN VALVE ! ; 13 : CLOSE ( n -- ) VALVE @ SWAP - 0 MAX VAVLE ! ; 14 15 Screen 15 not modified 0 \ ANSWER TO QUESTION 5: ( 5 MARKS ) 18:36JWB02/16/86 1 ( a) CREATE DATA 100 ALLOT 2 ( b) : CLEAR-DATA ( -- -- ) 3 DATA 100 0 FILL ; 4 ( a) VARIABLE N VARIABLE SSUM VARIABLE SSQR 5 : DATA@ ( i -- ) 6 DATA SWAP 2* + @ ; 7 ( c) 8 : COUNT-DATA ( -- -- ) 0 N ! 9 50 0 DO I DATA@ IF 1 N +! THEN LOOP ; 10 ( d) 11 : SUM-DATA ( -- -- ) 0 SSUM ! 12 50 0 DO I DATA@ ?DUP IF SSUM +! THEN LOOP ; 13 ( e) 14 : SUM-SQR ( -- -- ) 0 SSQR ! 15 50 0 DO I DATA@ ?DUP IF DUP * SSQR +! THEN LOOP ; --> Screen 16 not modified 0 \ Used for testing solution. 18:34JWB02/16/86 1 2 : #IN QUERY INTERPRET ; 3 : ENTER-DATA ( -- -- ) 4 CLEAR-DATA 0 BEGIN 5 CR DUP . ASCII > EMIT #IN DUP 0<> 6 WHILE OVER 2* DATA + ! 1+ REPEAT 2DROP ; 7 8 9 10 11 12 13 14 15
projects/test1-2.blk.txt · Zuletzt geändert: 2013-06-06 21:27 von 127.0.0.1