Benutzer-Werkzeuge

Webseiten-Werkzeuge


projects:test1-3.blk

MID TERM EXAM 3

Screen 0 not modified     
 0 \                                              13:36JWB02/16/86 
 1 \ Last change:   Screen  002                   11:21jwb10/22/87 
 2                                                                 
 3                      MATHEMATICS  495                           
 4                                                                 
 5                                                                 
 6         INTRODUCTION TO THE FORTH PROGRAMMING LANGUAGE          
 7                                                                 
 8                                                                 
 9                       MID TERM EXAM                             
10                                                                 
11                     OCTOBER 26, 1987                            
12                                                                 
13                     TOTAL  MARKS = 25                           
14                                                                 
15                                                                 


Screen 1 not modified     
 0 \ QUESTION 1 ( 5 MARKS )                       11:02jwb10/22/87 
 1 The following words are not part of the Forth 83 standard.      
 2 Write short colon definitions for them using only words from    
 3 your forth 83 reference sheet.                                  
 4                                                                 
 5 Example:      3DUP ( a b c   a b c a b c )                      
 6 Solution:   : 3DUP  2 PICK 2 PICK 2 PICK ;                      
 7             stack    stack                                      
 8     word    before   after                                      
 9     ----    ------   -------                                    
10 a)  TUCK   ( a b c   a c b c )                                  
11 b)  -ROT   ( a b c   c a b )                                    
12 c)  2OVER1 ( a b c   a b c a b )                                
13 d)  SPIN   ( a b c   c b a )                                    
14 e)  NIP    ( a b c   a c )                                      
15                                                                 


Screen 2 not modified     
 0 \ QUESTION 2 ( 5 MARKS )                       11:21jwb10/22/87 
 1 Design the FORTH word TRIANGLE that will draw the outline for   
 2 a triangle on the display using X's  TRIANGLE uses and consumes 
 3 the top stack number to decide how many lines will be used to   
 4 make the isosceles triangle. See the sample output below left.  
 5                    | Notes:                                     
 6 3 TRIANGLE         |         Your solution should illustrate    
 7   X                | the concept of factoring ( more than one   
 8  XXX               | word definition should be created).        
 9 XXXXX              |                                            
10 5 TRIANGLE         |      Your solution should illustrate either
11     X              |  the use of the DO .... LOOP  construct    
12    XXX             |  or  the     BEGIN .... UNTIL construct.   
13   XXXXX            |                                            
14  XXXXXXX           |      Your word should work for values from 
15 XXXXXXXXX          | 1 thru 23.  No action results for others.  


Screen 3 not modified     
 0 \ QUESTION 3 ( 5 MARKS )                       16:15JWB02/16/86 
 1 A open rectangular tank has a square ends, b meters by b meters,
 2 and is l meters long.                                           
 3 a) Write the word AREA that takes two stack inputs base, and    
 4    length and leaves the surface area, a= 2bb +3bh, on the stack
 5    AREA  ( b l   a )                                            
 6 b) Write the word VOLUME that takes two stack inputs base, and  
 7    length and leaves the volume, v=bbh, on the stack.           
 8    VOLUME ( b l  v )                                            
 9 c) Write the word TANK that takes two stack inputs base, and    
10    length and uses the words AREA and VOLUME above to produce   
11    a report. For example, 2 3 TANK should give the report below.
12    Square end of the tank is   2  meters.                       
13    Length of the tank is   3  meters.                           
14    The surface area of the tank is   26  square meters.         
15    THe volume of the tank is   12  cubic meters.                


Screen 4 not modified     
 0 \ QUESTION 4 ( 5 MARKS )                       16:49JWB10/27/85 
 1 a) Show how you would create a variable called  VALVE-SETTING . 
 2    The VALVE-SETTING will be used to contain the percentage     
 3    that a valve is open ( 0 is closed, 100 full open).          
 4 b) Design the word VALVE-STATUS to display the current value of 
 5    this variable.   Execution of VALVE-STATUS would display:    
 6    Valve is now  XX percent open.                               
 7 c) Write  %VALVE-OPEN  that sets  variable   VALVE-SETTING      
 8    to the top stack number.  Issue a warning and do nothing     
 9    if an attempt  is made  to set it outside the range 0 - 100. 
10 d) Write %VALVE-INC which increments VALVE-SETTING by top stack 
11    number and %VALVE-DEC which decrements it by the top stack #.
12 e) Can the VALVE-SETTING ever become less than 0 or greater     
13    than 100 using your words of (d).  If so fix them so they    
14    do nothing and issue a warning message if an attempt to      
15    increment or decrement out of range is made.                 


Screen 5 not modified     
 0 \ QUESTION 5 ( 5 MARKS )                       16:19JWB02/16/86 
 1 a) Create an array called MARKS that will hold 5 16bit numbers. 
 2 b) Write a word called CLEAR-MARKS which initializes            
 3    each cell of the array MARKS to zero.                        
 4 c) Write a word called SHOW-MARKS that displays and identifies  
 5    each value  in the MARKS array in tabular format:            
 6    1   1st mark                                                 
 7    2   2nd mark   etc.                                          
 8 d) Write a word called TOTAL  that computes the sum of all non  
 9    zero entries in the array MARKs and leaves the result in     
10    the variable SUM .  Save the non zero count in variable N .  
11 e) Write a word called MARK-AVG that computes the average of    
12    all non zero entries in the array MARKS and stores the       
13    result in the variable AVERAGE and displays the result as:   
14    Your current marks average is  nnn                           
15    MARK-AVG should round correctly to the nearest integer.      

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 a)  TUCK   ( a b c   a c b c )                                  
 2         SWAP OVER ;                                             
 3                                                                 
 4 b)  -ROT   ( a b c   c a b )                                    
 5         ROT ROT ;                                               
 6                                                                 
 7 c)  2OVER1 ( a b c   a b c a b )                                
 8         2 PICK 2 PICK ;                                         
 9                                                                 
10 d)  SPIN   ( a b c   c b a )                                    
11          SWAP ROT ;                                             
12                                                                 
13 e)  NIP    ( a b c   a c )                                      
14         SWAP DROP ;                                             
15                                                                 


Screen 12 not modified     
 0 \ ANSWER TO QUESTION NUMBER 2  ( 5 MARKS )     20:25JWB02/17/86 
 1 : X'S  ( n   -- )                                               
 2       0 ?DO ASCII X EMIT LOOP ;                                 
 3                                                                 
 4 : .ROW ( n r   -- )                                             
 5       2DUP - SPACES 2 * 1- X'S DROP ;                           
 6                                                                 
 7 : (TRIANGLE) ( n  -- )                                          
 8       DUP 0 ?DO  CR DUP I 1+ .ROW LOOP DROP ;                   
 9                                                                 
10 : TRIANGLE  ( n   -- )                                          
11       DUP 1 < IF   DROP ." Zero or negative not allowed"        
12               ELSE DUP 23 <                                     
13                    IF (TRIANGLE)                                
14                    ELSE DROP ." Larger than 23 not allowed"     
15               THEN THEN ;                                       


Screen 13 not modified     
 0 \ ANSWER TO QUESTION 3  ( 5 MARKS )            18:04JWB02/16/86 
 1  : AREA ( b l   a )                                             
 2      OVER DUP *  2 * ROT ROT  * 3 * +  ;                        
 3  : VOLUME ( b l   v )                                           
 4      OVER * *  ;                                                
 5                                                                 
 6  :  TANK ( b l  --  )                                           
 7    CR OVER ." The square end of the tank is " . ." meters."     
 8    CR DUP  ." The length of the tank is " . ." meters."         
 9       OVER OVER                                                 
10    CR ." The surface area of the tank is " AREA .               
11       ." square meters."                                        
12    CR ." The volume of the tank is " VOLUME .                   
13       ." cubic meters."                                         
14    CR  ;                                                        
15     2   3    TANK                                               


Screen 14 not modified     
 0 \ ANSWER TO QUESTION 4 ( 5 MARKS)              20:22JWB02/17/86 
 1 ( a) VARIABLE  VALVE-SETTING                                    
 2 ( b) : VALVE-STATUS ( --  -- ) CR ." Valve is now  "            
 3          VALVE-SETTING @ . ." percent open." ;                  
 4 ( c) : [IN]  ( x a b   f)  2 PICK < NOT ROT ROT < NOT AND ;     
 5      : WARN  ( --  -- ) ." Invalid operation attempted. " ;     
 6      : %VALVE-OPEN  ( n  -- )                                   
 7        DUP 0 100 [IN] IF  VALVE-SETTING ! ELSE DROP WARN THEN ; 
 8 ( d) : %VALVE-INC   ( n  -- ) VALVE-SETTING +! ;                
 9      : %VALVE-DEC   ( n  -- ) NEGATE  VALVE-SETTING +! ;        
10 ( e) : %VALVE-INC   ( n  -- )                                   
11         VALVE-SETTING @ +  DUP 0 100 [IN]                       
12         IF VALVE-SETTING ! ELSE DROP WARN THEN ;                
13      : %VALVE-DEC   ( n  -- )                                   
14         VALVE-SETTING @ SWAP - DUP 0 100 [IN]                   
15         IF VALVE-SETTING ! ELSE DROP WARN THEN ;                


Screen 15 not modified     
 0 \ ANSWER TO QUESTION 5:  ( 5 MARKS )           18:36JWB02/16/86 
 1 ( a) CREATE MARKS  70 ,  0 ,  80 , 0 , 65 ,   \ average=72      
 2 ( b)  : CLEAR-MARKS ( --  -- )                                  
 3          MARKS  10 0  FILL ;                                    
 4       VARIABLE N   VARIABLE SUM     VARIABLE AVERAGE            
 5  : MARKS@ ( i  -- )                                             
 6     MARKS SWAP 2* + @ ;                                         
 7 ( c)                                                            
 8  : SHOW-MARKS ( --   -- )                                       
 9    5 0 DO CR I 1+  . I MARKS@ 4 .R LOOP  ;                      
10 ( d)                                                            
11  : TOTAL ( --   -- ) 0 SUM ! 0 N !                              
12    5 0 DO I MARKS@ ?DUP IF SUM +! 1 N +!  THEN  LOOP ;          
13 ( e) : MARKS-AVG  ( --  -- )                                    
14    TOTAL SUM @ 2 * N @ / 1+ 2 / AVERAGE !                       
15    CR ." Your current marks average is " AVERAGE @ . ;          


Screen 16 not modified     
 0 \  Used for testing solution.                  18:34JWB02/16/86 
 1                                                                 
 2      : #IN  QUERY  INTERPRET ;                                  
 3      : ENTER-MARKS ( --  -- )                                   
 4        CLEAR-MARKS  5 0  DO                                     
 5        CR I 1+ . ASCII > EMIT  #IN                              
 6        I 2* MARKS + ! LOOP ;                                    
 7                                                                 
 8                                                                 
 9                                                                 
10                                                                 
11                                                                 
12                                                                 
13                                                                 
14                                                                 
15                                                                 
projects/test1-3.blk.txt · Zuletzt geändert: 2013-06-06 21:27 (Externe Bearbeitung)