Benutzer-Werkzeuge

Webseiten-Werkzeuge


projects:test1-1.blk

MID TERM EXAM

Screen 0 not modified     
 0                                                                 
 1 \ Last change:   Screen  005                   16:49JWB10/27/85 
 2                                                                 
 3                      MATHEMATICS  495                           
 4                                                                 
 5                                                                 
 6         INTRODUCTION TO THE FORTH PROGRAMMING LANGUAGE          
 7                                                                 
 8                                                                 
 9                       MID TERM EXAM                             
10                                                                 
11                     TOTAL  MARKS = 25                           
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 1 not modified     
 0 \ QUESTION 1 ( 5 MARKS )                       16:48JWB10/27/85 
 1 Show the resulting stack after return is pressed.               
 2                                                                 
 3 Example:   54  21  13   SWAP                                    
 4  Answer:   54  13  21    <top of stack.                         
 5                                                                 
 6 a)  13  17  23  31   ROT                                        
 7                                                                 
 8 b)  13  31  17  23   OVER                                       
 9                                                                 
10 c)  23  17  13   2   PICK                                       
11                                                                 
12 d)  11  5  -14  -5   /MOD                                       
13                                                                 
14 e)  10  20   7   5   */                                         
15                                                                 


Screen 2 not modified     
 0 \ QUESTION 2 ( 5 MARKS )                       16:48JWB10/27/85 
 1 Design a FORTH word SQUARE that will draw  the outline of a     
 2 square on the display.  The outline will be made by using the   
 3 word  WHITE  ( or WW )  from out CHECKER-BOARD  program.        
 4 Note:  Center of the square should be black!!                   
 5                                                                 
 6 : WHITE   177  EMIT  ;  ( Fills  character cell with white )    
 7                                                                 
 8         DO EITHER  a)  OR  b)   BELOW                           
 9                                                                 
10 a) Easy Version:    SQUARE  takes no stack parameters and draws 
11    a square   20 horizontal cells by 10 vertical cells.         
12                                                                 
13 b) Harder Version:  SQUARE  uses top stack number, n, and draws 
14    a square 2n cells horizontal  and n cells vertical.          
15                                                                 


Screen 3 not modified     
 0 \ QUESTION 3 ( 5 MARKS )                       16:47JWB10/27/85 
 1 An open fish tank has a square base ( b inches by b inches )    
 2 and is h inches high.  Design the word  TANK  ,  which takes    
 3 two stack inputs,  height and base,  and  computes the tank     
 4 surface area and tank volume.   V = bbh    S = 4bh + bb         
 5 Output display for tank should be similar to the sample below:  
 6                                                                 
 7 18  24   TANK                                                   
 8                                                                 
 9 Base  of  tank is  24 inches.                                   
10 Height of tank is  18 inches.                                   
11                                                                 
12 Surface  area  is   2304 square inches.                         
13 Volume of tank is  10368 cubic  inches.                         
14                                                                 
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  BALANCE   
 2    that will contain the number of dollars in your bank account.
 3                                                                 
 4 b) What are the FORTH words for fetching,  storing and          
 5    incrementing the contents of variables?  Give examples of    
 6    their use with  BALANCE  .                                   
 7                                                                 
 8 c) Design the word  BALANCE?  that displays current balance.    
 9    ie    BALANCE?    displays:   You now have  XXXX   dollars.  
10 d) Write  OPENING-BALANCE   that sets  variable   BALANCE       
11    to the top stack number.                                     
12                                                                 
13 e) Design the word DEPOSITE  which adds  top of stack to BALANCE
14    and word WITHDRAW which subtracts top of stack from BALANCE .
15    Can you over draw your account?  Fix WITHDRAW so you can't.  


Screen 5 not modified     
 0 \ QUESTION 5 ( 5 MARKS )                       16:49JWB10/27/85 
 1 a) Create and 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 SUM-DATA accumulates the sum of          
 7    the positive DATA values in the variable +SUM and the sum of 
 8    the negative DATA values in the variable -SUM .              
 9                                                                 
10 d) Write a word called ENTER-DATA that uses a indefinite loop   
11    ( ...  BEGIN ...  WHILE ... REPEAT .. )  to prompt the       
12    user to enter data and stores it in succesive cells of the   
13    DATA  array.  Fix it so that an entry of zero terminates     
14    data entry.   Assume you have the word #IN in your system.   
15                                                                 


Screen 6 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 7 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 1  ( 5 MARKS )                               
 1 Show the resulting stack after return is pressed.               
 2                                                                 
 3 Example:   54  21  13   SWAP                                    
 4  Answer:   54  13  21    <top of stack.                         
 5                                 Answers below:                  
 6 a)  13  17  23  31   ROT        13  23  31  17                  
 7                                                                 
 8 b)  13  31  17  23   OVER       13  31  17  23  17              
 9                                                                 
10 c)  23  17  13   2   PICK       23  17  13  23                  
11                                                                 
12 d)  11  5  -14  -5   /MOD       11   5  -4   2                  
13                                                                 
14 e)  10  20   7   5   */         10  28                          
15                                                                 


Screen 12 not modified     
 0 \ ANSWER TO QUESTION 2  ( 5 MARKS )                             
 1 : WW   177  EMIT  ;  ( Fills  character cell with white )       
 2 \  a) Easy Version:                                             
 3    : TOP   CR 20 0 DO  WW  LOOP ;                               
 4    : ROW   CR WW 18 0 DO SPACE LOOP WW ;                        
 5    : SQUARE  TOP  8 0 DO ROW LOOP TOP CR CR ;                   
 6                                                                 
 7 \  b) Harder Version:                                           
 8    : .TOP  ( n  -- )                                            
 9         CR 2* 0 ?DO WW LOOP ;                                   
10    : .ROW  ( n  -- )                                            
11         CR WW 2* 2- 0 ?DO SPACE LOOP WW ;                       
12    : .SQUARE ( n  -- )                                          
13          DUP 2 < ABORT" Can't do it!!"                          
14          DUP .TOP  DUP  2- 0                                    
15          ?DO  DUP .ROW LOOP .TOP CR CR ;                        


Screen 13 not modified     
 0 \ ANSWER TO QUESTION 3  ( 5 MARKS )                             
 1                                                                 
 2  : .BH  ( h b  -- )                                             
 3      CR ." Base  of  tank is"  4 .R    ."  inches."             
 4      CR ." Height of tank is"  4 .R    ."  inches."   ;         
 5  : .AREA ( h b  -- )                                            
 6       DUP DUP *  ROT ROT  * 4 * +                               
 7       CR ." Surface  area  is"  8 .R    ."  square inches."  ;  
 8  : .VOL  ( h b  -- )                                            
 9       DUP * *                                                   
10       CR ." Volume of tank is" 8 .R ."  cubic  inches."  ;      
11  :  TANK ( h b  --  )                                           
12         CR 2DUP .BH  CR 2DUP .AREA .VOL CR CR ;                 
13     18  24   TANK                                               
14                                                                 
15                                                                 


Screen 14 not modified     
 0 \ ANSWER TO QUESTION 4  ( 5 MARKS )                             
 1 ( a)    VARIABLE  BALANCE                                       
 2 \ b  fetching  @   storing !   incrementing  +!                 
 3 \    BALANCE  @    ( leave current value of BALANCE on stack )  
 4 \  5 BALANCE  !    ( initialize BALANCE to 5 dollars )          
 5 \  2 BALANCE +!    ( increment  BALANCE by 2 dollars )          
 6 ( c) : BALANCE?  ( --   -- )                                    
 7          BALANCE @  CR  ."  You now have "  .  ." dollars." ;   
 8 ( d) : OPENING-BALANCE  ( n  -- )                               
 9          BALANCE ! ;                                            
10 ( e)  : DEPOSITE ( n  -- )                                      
11             BALANCE +!   ;                                      
12       : WITHDRAW ( n  -- )                                      
13          BALANCE @  OVER - 0<                                   
14          IF DROP ." Insufficient funds!"                        
15          ELSE  NEGATE BALANCE +! THEN   ;                       


Screen 15 not modified     
 0 \ ANSWER TO QUESTION 5:  ( 5 MARKS )                            
 1 ( a) CREATE  DATA  100 ALLOT                                    
 2 ( b)  : CLEAR-DATA ( --  -- )                                   
 3           DATA  100 0  FILL ;                                   
 4 ( a)  VARIABLE +SUM     VARIABLE -SUM                           
 5  : SUM-DATA  ( --   -- )                                        
 6    0 +SUM !  0 -SUM !                                           
 7    50 0 DO DATA I 2* + @  DUP 0= IF DROP LEAVE THEN             
 8            DUP  0< IF -SUM ELSE +SUM THEN +!  LOOP ;            
 9                                                                 
10      : #IN  QUERY  INTERPRET ;                                  
11 ( d) : ENTER-DATA ( --  -- )                                    
12        CLEAR-DATA  0  BEGIN                                     
13        CR DUP . ASCII > EMIT  #IN DUP 0<>                       
14        WHILE  OVER 2* DATA + ! 1+ REPEAT  2DROP ;               
15                                                                 
projects/test1-1.blk.txt · Zuletzt geändert: 2013-06-06 21:27 von 127.0.0.1